Skip to content

Commit

Permalink
same treatment for Expr.sort
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 15, 2025
1 parent 655616e commit 7b6738d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 77 deletions.
91 changes: 14 additions & 77 deletions narwhals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,90 +1691,27 @@ def replace_strict(
def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self:
"""Sort this column. Place null values first.
!!! warning
`Expr.sort` is deprecated and will be removed in a future version.
Hint: instead of `df.select(nw.col('a').sort())`, use
`df.select(nw.col('a')).sort()` instead.
Note: this will remain available in `narwhals.stable.v1`.
See [stable api](../backcompat.md/) for more information.
Arguments:
descending: Sort in descending order.
nulls_last: Place null values last instead of first.
Returns:
A new expression.
Examples:
>>> import pandas as pd
>>> import polars as pl
>>> import pyarrow as pa
>>> import narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>>
>>> data = {"a": [5, None, 1, 2]}
>>> df_pd = pd.DataFrame(data)
>>> df_pl = pl.DataFrame(data)
>>> df_pa = pa.table(data)
Let's define dataframe-agnostic functions:
>>> def agnostic_sort(df_native: IntoFrameT) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("a").sort()).to_native()
>>> def agnostic_sort_descending(df_native: IntoFrameT) -> IntoFrameT:
... df = nw.from_native(df_native)
... return df.select(nw.col("a").sort(descending=True)).to_native()
We can then pass any supported library such as pandas, Polars, or
PyArrow to `agnostic_sort` and `agnostic_sort_descending`:
>>> agnostic_sort(df_pd)
a
1 NaN
2 1.0
3 2.0
0 5.0
>>> agnostic_sort(df_pl)
shape: (4, 1)
┌──────┐
│ a │
│ --- │
│ i64 │
╞══════╡
│ null │
│ 1 │
│ 2 │
│ 5 │
└──────┘
>>> agnostic_sort(df_pa)
pyarrow.Table
a: int64
----
a: [[null,1,2,5]]
>>> agnostic_sort_descending(df_pd)
a
1 NaN
0 5.0
3 2.0
2 1.0
>>> agnostic_sort_descending(df_pl)
shape: (4, 1)
┌──────┐
│ a │
│ --- │
│ i64 │
╞══════╡
│ null │
│ 5 │
│ 2 │
│ 1 │
└──────┘
>>> agnostic_sort_descending(df_pa)
pyarrow.Table
a: int64
----
a: [[null,5,2,1]]
"""
msg = (
"`Expr.sort` is deprecated and will be removed in a future version.\n\n"
"Hint: instead of `df.select(nw.col('a').sort())`, use `df.select(nw.col('a')).sort()`.\n\n"
"Note: this will remain available in `narwhals.stable.v1`.\n"
"See [stable api](../backcompat.md/) for more information.\n"
)
issue_deprecation_warning(msg, _version="1.22.0")
return self.__class__(
lambda plx: self._to_compliant_expr(plx).sort(
descending=descending, nulls_last=nulls_last
Expand Down
6 changes: 6 additions & 0 deletions tests/expr_and_series/sort_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

import narwhals as nw_main
import narwhals.stable.v1 as nw
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data
Expand All @@ -29,6 +30,11 @@ def test_sort_expr(
nw.col("b").sort(descending=descending, nulls_last=nulls_last),
)
assert_equal_data(result, expected)
with pytest.deprecated_call():
df.select(
"a",
nw_main.col("b").sort(descending=descending, nulls_last=nulls_last),
)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 7b6738d

Please sign in to comment.