Skip to content

BUG: pytz.AmbiguousTimeError not caught in hypothesis test test_on_offset_implementations (GH41906) #41907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions pandas/tests/tseries/offsets/test_dst.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import timedelta

import pytest
import pytz

from pandas._libs.tslibs import Timestamp
from pandas._libs.tslibs.offsets import (
Expand All @@ -15,6 +16,7 @@
BYearEnd,
CBMonthBegin,
CBMonthEnd,
CustomBusinessDay,
DateOffset,
Day,
MonthBegin,
Expand Down Expand Up @@ -173,3 +175,51 @@ def test_all_offset_classes(self, tup):
first = Timestamp(test_values[0], tz="US/Eastern") + offset()
second = Timestamp(test_values[1], tz="US/Eastern")
assert first == second


@pytest.mark.xfail(
strict=False, reason="'Africa/Kinshasa' test case fails under pytz=2017.3"
)
@pytest.mark.parametrize(
"original_dt, target_dt, offset, tz",
[
(
Timestamp("1900-01-01"),
Timestamp("1905-07-01"),
MonthBegin(66),
"Africa/Kinshasa",
), # GH41906
(
Timestamp("2021-10-01 01:15"),
Timestamp("2021-10-31 01:15"),
MonthEnd(1),
"Europe/London",
),
(
Timestamp("2010-12-05 02:59"),
Timestamp("2010-10-31 02:59"),
SemiMonthEnd(-3),
"Europe/Paris",
),
(
Timestamp("2021-10-31 01:20"),
Timestamp("2021-11-07 01:20"),
CustomBusinessDay(2, weekmask="Sun Mon"),
"US/Eastern",
),
(
Timestamp("2020-04-03 01:30"),
Timestamp("2020-11-01 01:30"),
YearBegin(1, month=11),
"America/Chicago",
),
],
)
def test_nontick_offset_with_ambiguous_time_error(original_dt, target_dt, offset, tz):
# .apply for non-Tick offsets throws AmbiguousTimeError when the target dt
# is dst-ambiguous
localized_dt = original_dt.tz_localize(tz)

msg = f"Cannot infer dst time from {target_dt}, try using the 'ambiguous' argument"
with pytest.raises(pytz.AmbiguousTimeError, match=msg):
localized_dt + offset
9 changes: 6 additions & 3 deletions pandas/tests/tseries/offsets/test_offsets_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ def test_on_offset_implementations(dt, offset):
# (dt + offset) - offset == dt
try:
compare = (dt + offset) - offset
except pytz.NonExistentTimeError:
# dt + offset does not exist, assume(False) to indicate
# to hypothesis that this is not a valid test case
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
# When dt + offset does not exist or is DST-ambiguous, assume(False) to
# indicate to hypothesis that this is not a valid test case
# DST-ambiguous example (GH41906):
# dt = datetime.datetime(1900, 1, 1, tzinfo=pytz.timezone('Africa/Kinshasa'))
# offset = MonthBegin(66)
assume(False)

assert offset.is_on_offset(dt) == (compare == dt)
Expand Down