Skip to content

Commit 16201ae

Browse files
jackzyliuJulianWgs
authored andcommitted
BUG: pytz.AmbiguousTimeError not caught in hypothesis test test_on_offset_implementations (GH41906) (pandas-dev#41907)
1 parent e735d2f commit 16201ae

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

pandas/tests/tseries/offsets/test_dst.py

+50
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import timedelta
55

66
import pytest
7+
import pytz
78

89
from pandas._libs.tslibs import Timestamp
910
from pandas._libs.tslibs.offsets import (
@@ -15,6 +16,7 @@
1516
BYearEnd,
1617
CBMonthBegin,
1718
CBMonthEnd,
19+
CustomBusinessDay,
1820
DateOffset,
1921
Day,
2022
MonthBegin,
@@ -173,3 +175,51 @@ def test_all_offset_classes(self, tup):
173175
first = Timestamp(test_values[0], tz="US/Eastern") + offset()
174176
second = Timestamp(test_values[1], tz="US/Eastern")
175177
assert first == second
178+
179+
180+
@pytest.mark.xfail(
181+
strict=False, reason="'Africa/Kinshasa' test case fails under pytz=2017.3"
182+
)
183+
@pytest.mark.parametrize(
184+
"original_dt, target_dt, offset, tz",
185+
[
186+
(
187+
Timestamp("1900-01-01"),
188+
Timestamp("1905-07-01"),
189+
MonthBegin(66),
190+
"Africa/Kinshasa",
191+
), # GH41906
192+
(
193+
Timestamp("2021-10-01 01:15"),
194+
Timestamp("2021-10-31 01:15"),
195+
MonthEnd(1),
196+
"Europe/London",
197+
),
198+
(
199+
Timestamp("2010-12-05 02:59"),
200+
Timestamp("2010-10-31 02:59"),
201+
SemiMonthEnd(-3),
202+
"Europe/Paris",
203+
),
204+
(
205+
Timestamp("2021-10-31 01:20"),
206+
Timestamp("2021-11-07 01:20"),
207+
CustomBusinessDay(2, weekmask="Sun Mon"),
208+
"US/Eastern",
209+
),
210+
(
211+
Timestamp("2020-04-03 01:30"),
212+
Timestamp("2020-11-01 01:30"),
213+
YearBegin(1, month=11),
214+
"America/Chicago",
215+
),
216+
],
217+
)
218+
def test_nontick_offset_with_ambiguous_time_error(original_dt, target_dt, offset, tz):
219+
# .apply for non-Tick offsets throws AmbiguousTimeError when the target dt
220+
# is dst-ambiguous
221+
localized_dt = original_dt.tz_localize(tz)
222+
223+
msg = f"Cannot infer dst time from {target_dt}, try using the 'ambiguous' argument"
224+
with pytest.raises(pytz.AmbiguousTimeError, match=msg):
225+
localized_dt + offset

pandas/tests/tseries/offsets/test_offsets_properties.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ def test_on_offset_implementations(dt, offset):
100100
# (dt + offset) - offset == dt
101101
try:
102102
compare = (dt + offset) - offset
103-
except pytz.NonExistentTimeError:
104-
# dt + offset does not exist, assume(False) to indicate
105-
# to hypothesis that this is not a valid test case
103+
except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError):
104+
# When dt + offset does not exist or is DST-ambiguous, assume(False) to
105+
# indicate to hypothesis that this is not a valid test case
106+
# DST-ambiguous example (GH41906):
107+
# dt = datetime.datetime(1900, 1, 1, tzinfo=pytz.timezone('Africa/Kinshasa'))
108+
# offset = MonthBegin(66)
106109
assume(False)
107110

108111
assert offset.is_on_offset(dt) == (compare == dt)

0 commit comments

Comments
 (0)