Skip to content

Commit 2dacbb2

Browse files
authored
test: polars categorical physical order deprecation and (temporarely) xfail datetime selectors (#2922)
* test: xfail polars datetime selectors * ordered cat test coverage * address categorical deprecation * inline comment * improve note, bump typing version
1 parent bf5bede commit 2dacbb2

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

narwhals/_utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
is_numpy_array_1d_int,
4444
is_pandas_like_dataframe,
4545
is_pandas_like_series,
46-
is_polars_series,
47-
is_pyarrow_chunked_array,
4846
)
4947
from narwhals.exceptions import ColumnNotFoundError, DuplicateError, InvalidOperationError
5048

@@ -1250,7 +1248,7 @@ def is_ordered_categorical(series: Series[Any]) -> bool:
12501248
>>> import polars as pl
12511249
>>> data = ["x", "y"]
12521250
>>> s_pd = pd.Series(data, dtype=pd.CategoricalDtype(ordered=True))
1253-
>>> s_pl = pl.Series(data, dtype=pl.Categorical(ordering="physical"))
1251+
>>> s_pl = pl.Series(data, dtype=pl.Categorical(ordering="lexical"))
12541252
12551253
Let's define a library-agnostic function:
12561254
@@ -1263,7 +1261,7 @@ def is_ordered_categorical(series: Series[Any]) -> bool:
12631261
>>> func(s_pd)
12641262
True
12651263
>>> func(s_pl)
1266-
True
1264+
False
12671265
"""
12681266
from narwhals._interchange.series import InterchangeSeries
12691267

@@ -1281,11 +1279,15 @@ def is_ordered_categorical(series: Series[Any]) -> bool:
12811279
result = False
12821280
else:
12831281
native = series.to_native()
1284-
if is_polars_series(native):
1282+
impl = series.implementation
1283+
if impl.is_polars() and impl._backend_version() < (1, 32):
1284+
# NOTE: Deprecated https://github.com/pola-rs/polars/pull/23779
1285+
# Since version 1.32.0, ordering parameter is ignored and
1286+
# it always behaves as if 'lexical' was passed.
12851287
result = cast("pl.Categorical", native.dtype).ordering == "physical"
1286-
elif is_pandas_like_series(native):
1288+
elif impl.is_pandas_like():
12871289
result = bool(native.cat.ordered)
1288-
elif is_pyarrow_chunked_array(native):
1290+
elif impl.is_pyarrow():
12891291
from narwhals._arrow.utils import is_dictionary
12901292

12911293
result = is_dictionary(native.type) and native.type.ordered

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typing = [ # keep some of these pinned and bump periodically so there's fewer s
7272
"pyright",
7373
"pyarrow-stubs==19.2",
7474
"sqlframe",
75-
"polars==1.30.0",
75+
"polars==1.32.0",
7676
"uv",
7777
"narwhals[ibis]",
7878
]

tests/selectors_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def test_datetime(constructor: Constructor, request: pytest.FixtureRequest) -> N
8989
or ("pandas" in str(constructor) and PANDAS_VERSION < (2,))
9090
or "ibis" in str(constructor)
9191
# https://github.com/pola-rs/polars/issues/23767
92-
or ("polars" in str(constructor) and POLARS_VERSION == (1, 32, 0, 1))
92+
# TODO(FBruzzesi): Manage polars version
93+
or ("polars" in str(constructor) and POLARS_VERSION >= (1, 32, 0))
9394
):
9495
request.applymarker(pytest.mark.xfail)
9596
if "modin" in str(constructor):

tests/series_only/is_ordered_categorical_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
import narwhals as nw
8-
from narwhals._utils import Version
8+
from narwhals._utils import Implementation, Version
99
from tests.utils import POLARS_VERSION
1010

1111
if TYPE_CHECKING:
@@ -15,6 +15,7 @@
1515

1616
class MockCompliantSeries:
1717
_version = Version.MAIN
18+
_implementation = Implementation.UNKNOWN
1819

1920
def __narwhals_series__(self) -> Any:
2021
return self
@@ -34,9 +35,9 @@ def test_is_ordered_categorical_polars() -> None:
3435

3536
s: IntoSeries | Any
3637
s = pl.Series(["a", "b"], dtype=pl.Categorical)
37-
if POLARS_VERSION < (1, 32):
38+
if POLARS_VERSION < (1, 32): # pragma: no cover
3839
assert nw.is_ordered_categorical(nw.from_native(s, series_only=True))
39-
else: # pragma: no cover
40+
else:
4041
# Post 1.32 there's no physical ordering for categoricals anymore.
4142
assert not nw.is_ordered_categorical(nw.from_native(s, series_only=True))
4243
s = pl.Series(["a", "b"], dtype=pl.Categorical(ordering="lexical"))

0 commit comments

Comments
 (0)