Skip to content

Commit

Permalink
perf: remove all loc[:, ...] where assignment does not occur for
Browse files Browse the repository at this point in the history
pandas backend
  • Loading branch information
aivanoved committed Sep 25, 2024
1 parent 0350661 commit 1eff960
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_column(self, name: str) -> PandasLikeSeries:
from narwhals._pandas_like.series import PandasLikeSeries

return PandasLikeSeries(
self._native_frame.loc[:, name],
self._native_frame[name],
implementation=self._implementation,
backend_version=self._backend_version,
)
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def from_column_names(
def func(df: PandasLikeDataFrame) -> list[PandasLikeSeries]:
return [
PandasLikeSeries(
df._native_frame.loc[:, column_name],
df._native_frame[column_name],
implementation=df._implementation,
backend_version=df._backend_version,
)
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, df: PandasLikeDataFrame, keys: list[str]) -> None:
self._df._implementation is Implementation.PANDAS
and self._df._backend_version < (1, 0)
): # pragma: no cover
if self._df._native_frame.loc[:, self._keys].isna().any().any():
if self._df._native_frame[self._keys].isna().any().any():
msg = "Grouping by null values is not supported in pandas < 1.0.0"
raise NotImplementedError(msg)
self._grouped = self._df._native_frame.groupby(
Expand Down Expand Up @@ -210,7 +210,7 @@ def agg_pandas( # noqa: PLR0915
else:
# No aggregation provided
result_aggs = native_namespace.DataFrame(grouped.groups.keys(), columns=keys)
return from_dataframe(result_aggs.loc[:, output_names])
return from_dataframe(result_aggs[output_names])

if dataframe_is_empty:
# Don't even attempt this, it's way too inconsistent across pandas versions.
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def all(self) -> PandasLikeExpr:
return PandasLikeExpr(
lambda df: [
PandasLikeSeries(
df._native_frame.loc[:, column_name],
df._native_frame[column_name],
implementation=self._implementation,
backend_version=self._backend_version,
)
Expand Down

0 comments on commit 1eff960

Please sign in to comment.