Skip to content

Commit

Permalink
feat: when calling convert_time_zone on time-zone-naive datetime, c…
Browse files Browse the repository at this point in the history
…onvert as if from UTC
  • Loading branch information
MarcoGorelli committed Jan 24, 2024
1 parent af19d13 commit 6f216c8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
8 changes: 2 additions & 6 deletions crates/polars-plan/src/dsl/function_expr/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,12 @@ pub(super) fn to_string(s: &Series, format: &str) -> PolarsResult<Series> {
#[cfg(feature = "timezones")]
pub(super) fn convert_time_zone(s: &Series, time_zone: &TimeZone) -> PolarsResult<Series> {
match s.dtype() {
DataType::Datetime(_, Some(_)) => {
DataType::Datetime(_, _) => {
let mut ca = s.datetime()?.clone();
ca.set_time_zone(time_zone.clone())?;
Ok(ca.into_series())
},
_ => polars_bail!(
ComputeError:
"cannot call `convert_time_zone` on tz-naive; set a time zone first \
with `replace_time_zone`"
),
dtype => polars_bail!(ComputeError: "expected Datetime, got {}", dtype),
}
}
pub(super) fn with_time_unit(s: &Series, tu: TimeUnit) -> PolarsResult<Series> {
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6395,7 +6395,7 @@ def join(
Notes
-----
For joining on columns with categorical data, see `pl.StringCache()`.
For joining on columns with categorical data, see :class:`pl.StringCache`.
"""
if not isinstance(other, DataFrame):
msg = f"expected `other` join table to be a DataFrame, got {type(other).__name__!r}"
Expand Down
5 changes: 5 additions & 0 deletions py-polars/polars/expr/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,11 @@ def convert_time_zone(self, time_zone: str) -> Expr:
time_zone
Time zone for the `Datetime` expression.
Notes
-----
If converting from a time-zone-naive datetime, then conversion will happen
as if converting from UTC, regardless of your system's time zone.
Examples
--------
>>> from datetime import datetime
Expand Down
5 changes: 5 additions & 0 deletions py-polars/polars/series/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,11 @@ def convert_time_zone(self, time_zone: str) -> Series:
time_zone
Time zone for the `Datetime` Series.
Notes
-----
If converting from a time-zone-naive datetime, then conversion will happen
as if converting from UTC, regardless of your system's time zone.
Examples
--------
>>> from datetime import datetime
Expand Down
8 changes: 3 additions & 5 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,11 +1579,9 @@ def test_convert_time_zone_lazy_schema() -> None:

def test_convert_time_zone_on_tz_naive() -> None:
ts = pl.Series(["2020-01-01"]).str.strptime(pl.Datetime)
with pytest.raises(
ComputeError,
match="cannot call `convert_time_zone` on tz-naive; set a time zone first with `replace_time_zone`",
):
ts.dt.convert_time_zone("Africa/Bamako")
result = ts.dt.convert_time_zone("Africa/Bamako").item()
expected = datetime(2020, 1, 1, 0, 0, tzinfo=ZoneInfo(key="Africa/Bamako"))
assert result == expected


def test_tz_aware_get_idx_5010() -> None:
Expand Down

0 comments on commit 6f216c8

Please sign in to comment.