Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nw.selectors.by_dtype(nw.Datetime) was excluding tz-aware for Polars #1869

Merged
merged 11 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 9 additions & 4 deletions narwhals/_polars/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from narwhals._expression_parsing import parse_into_exprs
from narwhals._polars.utils import extract_args_kwargs
from narwhals._polars.utils import narwhals_to_native_dtype
from narwhals.dtypes import DType
from narwhals.utils import Implementation

if TYPE_CHECKING:
Expand All @@ -22,7 +23,6 @@
from narwhals._polars.dataframe import PolarsLazyFrame
from narwhals._polars.expr import PolarsExpr
from narwhals._polars.typing import IntoPolarsExpr
from narwhals.dtypes import DType
from narwhals.utils import Version


Expand Down Expand Up @@ -219,10 +219,15 @@ def by_dtype(self: Self, dtypes: Iterable[DType]) -> PolarsExpr:

from narwhals._polars.expr import PolarsExpr

native_dtypes: list[pl.DataType | type[pl.DataType]] = []
for dtype in dtypes:
native_dtype_instantiated = narwhals_to_native_dtype(dtype, self._version)
if isinstance(dtype, type) and issubclass(dtype, DType):
native_dtypes.append(native_dtype_instantiated.__class__)
else:
native_dtypes.append(native_dtype_instantiated)
return PolarsExpr(
pl.selectors.by_dtype(
[narwhals_to_native_dtype(dtype, self._version) for dtype in dtypes]
),
Copy link
Member Author

Choose a reason for hiding this comment

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

the issue was that we were translating nw.Datetime to pl.Datetime(). In other parts of the codebase, that's fine. But here, we need to preserve whether the dtype is instantiated or not, as they behave differently

Like this, we keep the translation from nw.Datetime to pl.Datetime

pl.selectors.by_dtype(native_dtypes),
version=self._version,
backend_version=self._backend_version,
)
Expand Down
15 changes: 15 additions & 0 deletions tests/selectors_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import re
from datetime import datetime

import pytest

Expand Down Expand Up @@ -125,3 +126,17 @@ def test_set_ops_invalid(constructor: Constructor) -> None:
match=re.escape("unsupported operand type(s) for op: ('Selector' + 'Selector')"),
):
df.select(boolean() + numeric())


def test_tz_aware(constructor: Constructor, request: pytest.FixtureRequest) -> None:
if "polars" in str(constructor) and POLARS_VERSION < (1, 19):
# bug in old polars
request.applymarker(pytest.mark.xfail)

data = {"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)], "c": [4, 5]}
df = nw.from_native(constructor(data)).with_columns(
b=nw.col("a").dt.replace_time_zone("Asia/Katmandu")
)
result = df.select(nw.selectors.by_dtype(nw.Datetime)).collect_schema().names()
expected = ["a", "b"]
assert result == expected
Loading