Skip to content

Commit

Permalink
fix(python): tz-aware filtering (#5603)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 23, 2022
1 parent c35c3c3 commit c0fdc04
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
6 changes: 5 additions & 1 deletion py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,11 @@ def lit(
tu: TimeUnit
if isinstance(value, datetime):
tu = "us"
return lit(_datetime_to_pl_timestamp(value, tu)).cast(Datetime(tu))
e = lit(_datetime_to_pl_timestamp(value, tu)).cast(Datetime(tu))
if value.tzinfo is not None:
return e.dt.tz_localize(str(value.tzinfo))
else:
return e

elif isinstance(value, timedelta):
tu = "us"
Expand Down
41 changes: 41 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1996,3 +1996,44 @@ def test_tz_aware_strftime() -> None:
"Fri Nov 4 00:00:00 2022",
],
}


def test_tz_aware_filter_lit() -> None:
start = datetime(1970, 1, 1)
stop = datetime(1970, 1, 1, 7)
dt = datetime(1970, 1, 1, 6, tzinfo=zoneinfo.ZoneInfo("America/New_York"))

assert (
pl.DataFrame({"date": pl.date_range(start, stop, "1h")})
.with_column(pl.col("date").dt.tz_localize("America/New_York").alias("nyc"))
.filter(pl.col("nyc") < dt)
).to_dict(False) == {
"date": [
datetime(1970, 1, 1, 0, 0),
datetime(1970, 1, 1, 1, 0),
datetime(1970, 1, 1, 2, 0),
datetime(1970, 1, 1, 3, 0),
datetime(1970, 1, 1, 4, 0),
datetime(1970, 1, 1, 5, 0),
],
"nyc": [
datetime(
1970, 1, 1, 0, 0, tzinfo=zoneinfo.ZoneInfo(key="America/New_York")
),
datetime(
1970, 1, 1, 1, 0, tzinfo=zoneinfo.ZoneInfo(key="America/New_York")
),
datetime(
1970, 1, 1, 2, 0, tzinfo=zoneinfo.ZoneInfo(key="America/New_York")
),
datetime(
1970, 1, 1, 3, 0, tzinfo=zoneinfo.ZoneInfo(key="America/New_York")
),
datetime(
1970, 1, 1, 4, 0, tzinfo=zoneinfo.ZoneInfo(key="America/New_York")
),
datetime(
1970, 1, 1, 5, 0, tzinfo=zoneinfo.ZoneInfo(key="America/New_York")
),
],
}

0 comments on commit c0fdc04

Please sign in to comment.