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(python): Ensure the cs.temporal() selector uses wildcard time zone matching for Datetime #13683

Merged
merged 3 commits into from
Mar 21, 2024
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 py-polars/polars/datatypes/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ class Datetime(TemporalType):
time_zone
Time zone string, as defined in zoneinfo (to see valid strings run
`import zoneinfo; zoneinfo.available_timezones()` for a full list).
When using to match dtypes, can use "*" to check for Datetime columns
that have any timezone.
When used to match dtypes, can set this to "*" to check for Datetime
columns that have any (non-null) timezone.

Notes
-----
Expand Down
3 changes: 3 additions & 0 deletions py-polars/polars/datatypes/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
Datetime("ms"),
Datetime("us"),
Datetime("ns"),
Datetime("ms", "*"),
Datetime("us", "*"),
Datetime("ns", "*"),
]
)
DURATION_DTYPES: frozenset[PolarsDataType] = DataTypeGroup(
Expand Down
13 changes: 12 additions & 1 deletion py-polars/tests/unit/series/buffers/test_from_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
from polars.testing import assert_series_equal
from polars.testing.parametric import series

# TODO: Define data type groups centrally somewhere in the test suite
DATETIME_DTYPES: set[pl.PolarsDataType] = {
pl.Datetime,
pl.Datetime("ms"),
pl.Datetime("us"),
pl.Datetime("ns"),
}
TEMPORAL_DTYPES: set[pl.PolarsDataType] = (
{pl.Date, pl.Time} | pl.DURATION_DTYPES | DATETIME_DTYPES
)


@given(
s=series(
Expand All @@ -34,7 +45,7 @@ def test_series_from_buffers_numeric(s: pl.Series) -> None:
assert_series_equal(s, result)


@given(s=series(allowed_dtypes=pl.TEMPORAL_DTYPES, chunked=False))
@given(s=series(allowed_dtypes=TEMPORAL_DTYPES, chunked=False))
def test_series_from_buffers_temporal_with_validity(s: pl.Series) -> None:
validity = s.is_not_null()
physical = pl.Int32 if s.dtype == pl.Date else pl.Int64
Expand Down
37 changes: 37 additions & 0 deletions py-polars/tests/unit/test_selectors.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import sys
from datetime import datetime
from typing import Any

import pytest

import polars as pl
import polars.selectors as cs
from polars.dependencies import _ZONEINFO_AVAILABLE
from polars.selectors import expand_selector, is_selector
from polars.testing import assert_frame_equal

if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
elif _ZONEINFO_AVAILABLE:
# Import from submodule due to typing issue with backports.zoneinfo package:
# https://github.com/pganssle/zoneinfo/issues/125
from backports.zoneinfo._zoneinfo import ZoneInfo


def assert_repr_equals(item: Any, expected: str) -> None:
"""Assert that the repr of an item matches the expected string."""
Expand Down Expand Up @@ -356,6 +366,33 @@ def test_selector_temporal(df: pl.DataFrame) -> None:
assert df.select(cs.date() | cs.time()).schema == {"ghi": pl.Time, "JJK": pl.Date}


def test_selector_temporal_13665() -> None:
df = pl.DataFrame(
data={"utc": [datetime(1950, 7, 5), datetime(2099, 12, 31)]},
schema={"utc": pl.Datetime(time_zone="UTC")},
).with_columns(
idx=pl.int_range(0, 2),
utc=pl.col("utc").dt.replace_time_zone(None),
tokyo=pl.col("utc").dt.convert_time_zone("Asia/Tokyo"),
hawaii=pl.col("utc").dt.convert_time_zone("US/Hawaii"),
)
for selector in (cs.datetime(), cs.datetime("us"), cs.temporal()):
assert df.select(selector).to_dict(as_series=False) == {
"utc": [
datetime(1950, 7, 5, 0, 0),
datetime(2099, 12, 31, 0, 0),
],
"tokyo": [
datetime(1950, 7, 5, 10, 0, tzinfo=ZoneInfo(key="Asia/Tokyo")),
datetime(2099, 12, 31, 9, 0, tzinfo=ZoneInfo(key="Asia/Tokyo")),
],
"hawaii": [
datetime(1950, 7, 4, 14, 0, tzinfo=ZoneInfo(key="US/Hawaii")),
datetime(2099, 12, 30, 14, 0, tzinfo=ZoneInfo(key="US/Hawaii")),
],
}


def test_selector_expansion() -> None:
df = pl.DataFrame({name: [] for name in "abcde"})

Expand Down
Loading