Skip to content

Commit

Permalink
Test ambiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumiller committed Nov 12, 2024
1 parent def8577 commit 1e6b17d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/polars-time/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl PolarsReplaceDatetime for DatetimeChunked {
microsecond,
ambiguous,
&self.time_unit(),
None,
self.time_zone().as_deref(),
self.name(),
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,47 @@ def test_replace_int() -> None:
assert_frame_equal(result, expected)


def test_replace_ambiguous() -> None:
# Value to be replaced by an ambiguous hour.
value = pl.select(
pl.datetime(2020, 10, 25, 5, time_zone="Europe/London")
).to_series()

input = [2020, 10, 25, 1]
tz = "Europe/London"

# earliest
expected = pl.select(
pl.datetime(*input, time_zone=tz, ambiguous="earliest")
).to_series()
result = value.dt.replace(hour=1, ambiguous="earliest")
assert_series_equal(result, expected)

# latest
expected = pl.select(
pl.datetime(*input, time_zone=tz, ambiguous="latest")
).to_series()
result = value.dt.replace(hour=1, ambiguous="latest")
assert_series_equal(result, expected)

# null
expected = pl.select(
pl.datetime(*input, time_zone=tz, ambiguous="null")
).to_series()
result = value.dt.replace(hour=1, ambiguous="null")
assert_series_equal(result, expected)

# raise
with pytest.raises(
ComputeError,
match=(
"datetime '2020-10-25 01:00:00' is ambiguous in time zone 'Europe/London'. "
"Please use `ambiguous` to tell how it should be localized."
),
):
value.dt.replace(hour=1, ambiguous="raise")


@pytest.mark.parametrize(
("tzinfo", "time_zone"),
[(None, None), (ZoneInfo("Asia/Kathmandu"), "Asia/Kathmandu")],
Expand Down

0 comments on commit 1e6b17d

Please sign in to comment.