Skip to content

Commit

Permalink
fix: replace_time_zone with single-null-element "ambiguous" was panic…
Browse files Browse the repository at this point in the history
…king
  • Loading branch information
MarcoGorelli committed Mar 10, 2024
1 parent 06116df commit 3103639
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ pub fn replace_time_zone(
}),
};
let mut out = out?.into_datetime(datetime.time_unit(), time_zone.map(|x| x.to_string()));
if from_time_zone == "UTC" && ambiguous.len() == 1 && ambiguous.get(0).unwrap() == "raise" {
if from_time_zone == "UTC"
&& ambiguous.len() == 1
&& unsafe { ambiguous.get_unchecked(0) } == Some("raise")
{
// In general, the sortedness flag can't be preserved.
// To be safe, we only do so in the simplest case when we know for sure that there is no "daylight savings weirdness" going on, i.e.:
// - `from_tz` is guaranteed to not observe daylight savings time;
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,19 @@ def test_ambiguous_expressions() -> None:
assert_series_equal(result, expected)


def test_single_ambiguous_null() -> None:
df = pl.DataFrame(
{"ts": [datetime(2020, 10, 2, 1, 1)], "ambiguous": [None]},
schema_overrides={"ambiguous": pl.String},
)
result = df.select(
pl.col("ts").dt.replace_time_zone(
"Europe/London", ambiguous=pl.col("ambiguous")
)
)["ts"].item()
assert result is None


def test_unlocalize() -> None:
tz_naive = pl.Series(["2020-01-01 03:00:00"]).str.strptime(pl.Datetime)
tz_aware = tz_naive.dt.replace_time_zone("UTC").dt.convert_time_zone(
Expand Down

0 comments on commit 3103639

Please sign in to comment.