Skip to content

Commit

Permalink
fix(rust, python): reflect time zone conversion in lazy dataframe sch…
Browse files Browse the repository at this point in the history
…ema (pola-rs#7022)

Co-authored-by: MarcoGorelli <>
  • Loading branch information
MarcoGorelli authored and josemasar committed Feb 21, 2023
1 parent 3327a19 commit 79221a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion polars/polars-lazy/polars-plan/src/dsl/dt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl DateLikeNameSpace {
/// Change the underlying [`TimeZone`] of the [`Series`]. This does not modify the data.
#[cfg(feature = "timezones")]
pub fn convert_time_zone(self, time_zone: TimeZone) -> Expr {
let time_zone_clone = time_zone.clone();
self.0.map(
move |s| match s.dtype() {
DataType::Datetime(_, Some(_)) => {
Expand All @@ -79,7 +80,10 @@ impl DateLikeNameSpace {
"Cannot call convert_time_zone on tz-naive. Set a time zone first with replace_time_zone".into()
)),
},
GetOutput::same_type(),
GetOutput::map_dtype(move |dtype| match dtype {
DataType::Datetime(tu, _) => DataType::Datetime(*tu, Some(time_zone_clone.clone())),
_ => panic!("expected datetime"),
}),
)
}

Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,23 @@ def test_convert_time_zone_invalid() -> None:
ts.dt.replace_time_zone("UTC").dt.convert_time_zone("foo")


def test_convert_time_zone_lazy_schema() -> None:
ts_us = pl.Series(["2020-01-01"]).str.strptime(pl.Datetime("us", "UTC"))
ts_ms = pl.Series(["2020-01-01"]).str.strptime(pl.Datetime("ms", "UTC"))
ldf = pl.DataFrame({"ts_us": ts_us, "ts_ms": ts_ms}).lazy()
result = ldf.with_columns(
pl.col("ts_us").dt.convert_time_zone("America/New_York").alias("ts_us_ny"),
pl.col("ts_ms").dt.convert_time_zone("America/New_York").alias("ts_us_kt"),
).schema
expected = {
"ts_us": pl.Datetime("us", "UTC"),
"ts_ms": pl.Datetime("ms", "UTC"),
"ts_us_ny": pl.Datetime("us", "America/New_York"),
"ts_us_kt": pl.Datetime("ms", "America/New_York"),
}
assert result == expected


def test_convert_time_zone_on_tz_naive() -> None:
ts = pl.Series(["2020-01-01"]).str.strptime(pl.Datetime)
with pytest.raises(
Expand Down

0 comments on commit 79221a8

Please sign in to comment.