Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions narwhals/_arrow/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def func(df: ArrowDataFrame) -> Sequence[ArrowSeries]:
# TODO(marco): is there a way to do this efficiently without
# doing 2 sorts? Here we're sorting the dataframe and then
# again calling `sort_indices`. `ArrowSeries.scatter` would also sort.
sorting_indices = pc.sort_indices(df[token].native) # type: ignore[call-overload]
sorting_indices = pc.sort_indices(df.get_column(token).native) # type: ignore[call-overload]
Copy link
Member

@dangotbanned dangotbanned Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return [s._with_native(s.native.take(sorting_indices)) for s in result]
else:

Expand All @@ -184,7 +184,7 @@ def func(df: ArrowDataFrame) -> Sequence[ArrowSeries]:
right_on=partition_by,
suffix="_right",
)
return [tmp[alias] for alias in aliases]
return [tmp.get_column(alias) for alias in aliases]

return self.__class__(
func,
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_arrow/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def mode(self: Self) -> ArrowSeries:
return counts.filter(
plx.col(col_token)
== plx.col(col_token).max().broadcast(kind=ExprKind.AGGREGATION)
)[self.name]
).get_column(self.name)
Comment on lines 854 to +857
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return counts.filter(
plx.col(col_token)
== plx.col(col_token).max().broadcast(kind=ExprKind.AGGREGATION)
)[self.name]
).get_column(self.name)
column = plx.col(col_token)
predicate = column == column.max().broadcast(ExprKind.AGGREGATION)
return counts.filter(predicate).get_column(self.name)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ˜„ ah sorry, i jumped the gun

yeah much nicer this way πŸ‘

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries πŸ˜‰


def is_finite(self: Self) -> Self:
return self._with_native(pc.is_finite(self.native))
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ def to_numpy(self: Self, dtype: Any = None, *, copy: bool | None = None) -> _2DA

arr: Any = np.hstack(
[
self[col].to_numpy(copy=copy, dtype=None)[:, None]
self.get_column(col).to_numpy(copy=copy, dtype=None)[:, None]
for col in self.columns
]
)
Expand Down
10 changes: 5 additions & 5 deletions narwhals/_pandas_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def func(df: PandasLikeDataFrame) -> Sequence[PandasLikeSeries]:
*order_by, descending=False, nulls_last=False
)
results = self(df.drop([token], strict=True))
sorting_indices = df[token]
sorting_indices = df.get_column(token)
for s in results:
s._scatter_in_place(sorting_indices, s)
return results
Expand Down Expand Up @@ -257,14 +257,14 @@ def func(df: PandasLikeDataFrame) -> Sequence[PandasLikeSeries]:
columns = list(set(partition_by).union(output_names).union(order_by))
token = generate_temporary_column_name(8, columns)
df = (
df[columns]
df.simple_select(*columns)
.with_row_index(token)
.sort(*order_by, descending=reverse, nulls_last=reverse)
)
sorting_indices = df[token]
sorting_indices = df.get_column(token)
elif reverse:
columns = list(set(partition_by).union(output_names))
df = df[columns][::-1]
df = df.simple_select(*columns)[::-1]
grouped = df._native_frame.groupby(partition_by)
if function_name.startswith("rolling"):
rolling = grouped[list(output_names)].rolling(**pandas_kwargs)
Expand All @@ -287,7 +287,7 @@ def func(df: PandasLikeDataFrame) -> Sequence[PandasLikeSeries]:
result_frame = df._with_native(res_native).rename(
dict(zip(output_names, aliases))
)
results = [result_frame[name] for name in aliases]
results = [result_frame.get_column(name) for name in aliases]
if order_by:
for s in results:
s._scatter_in_place(sorting_indices, s)
Expand Down
Loading