From a6319c44abadcf7c5fc666bb636010d51b9e82fd Mon Sep 17 00:00:00 2001 From: Jack Liu <5811028+zheyuan-liu@users.noreply.github.com> Date: Wed, 9 Jun 2021 11:29:45 -0700 Subject: [PATCH 1/4] BUG: pytz.AmbiguousTimeError not caught in hypothesis test test_on_offset_implementations (GH41906) --- pandas/tests/tseries/offsets/test_offsets_properties.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/tseries/offsets/test_offsets_properties.py b/pandas/tests/tseries/offsets/test_offsets_properties.py index 8e0ace7775868..57dffd526a76e 100644 --- a/pandas/tests/tseries/offsets/test_offsets_properties.py +++ b/pandas/tests/tseries/offsets/test_offsets_properties.py @@ -100,7 +100,7 @@ def test_on_offset_implementations(dt, offset): # (dt + offset) - offset == dt try: compare = (dt + offset) - offset - except pytz.NonExistentTimeError: + except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError): # dt + offset does not exist, assume(False) to indicate # to hypothesis that this is not a valid test case assume(False) From 0477dc074cf8669ce9490994c3bb12ceff14e451 Mon Sep 17 00:00:00 2001 From: Jack Liu <5811028+zheyuan-liu@users.noreply.github.com> Date: Wed, 9 Jun 2021 12:44:12 -0700 Subject: [PATCH 2/4] add comment about AmbiguousTimeError --- pandas/tests/tseries/offsets/test_offsets_properties.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/tests/tseries/offsets/test_offsets_properties.py b/pandas/tests/tseries/offsets/test_offsets_properties.py index 57dffd526a76e..2d88f6690a794 100644 --- a/pandas/tests/tseries/offsets/test_offsets_properties.py +++ b/pandas/tests/tseries/offsets/test_offsets_properties.py @@ -101,8 +101,11 @@ def test_on_offset_implementations(dt, offset): try: compare = (dt + offset) - offset except (pytz.NonExistentTimeError, pytz.AmbiguousTimeError): - # dt + offset does not exist, assume(False) to indicate - # to hypothesis that this is not a valid test case + # 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) From 98dee29992a623d364cb90828a280465727a1582 Mon Sep 17 00:00:00 2001 From: Jack Liu <5811028+zheyuan-liu@users.noreply.github.com> Date: Fri, 11 Jun 2021 11:47:52 -0700 Subject: [PATCH 3/4] Add test for nontick offsets with ambiguous time error --- pandas/tests/tseries/offsets/test_dst.py | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pandas/tests/tseries/offsets/test_dst.py b/pandas/tests/tseries/offsets/test_dst.py index 0ae94b6b57640..cbd0aa4a47c9a 100644 --- a/pandas/tests/tseries/offsets/test_dst.py +++ b/pandas/tests/tseries/offsets/test_dst.py @@ -4,6 +4,7 @@ from datetime import timedelta import pytest +import pytz from pandas._libs.tslibs import Timestamp from pandas._libs.tslibs.offsets import ( @@ -15,6 +16,7 @@ BYearEnd, CBMonthBegin, CBMonthEnd, + CustomBusinessDay, DateOffset, Day, MonthBegin, @@ -173,3 +175,48 @@ 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.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 From 25f1368b6f2220423da3c01a47ebfefbaaeee81e Mon Sep 17 00:00:00 2001 From: Jack Liu <5811028+zheyuan-liu@users.noreply.github.com> Date: Mon, 14 Jun 2021 18:10:59 -0700 Subject: [PATCH 4/4] Mark test as xfail for now due to 'Africa/Kinshasa' failure under pytz=2017.3 --- pandas/tests/tseries/offsets/test_dst.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/tseries/offsets/test_dst.py b/pandas/tests/tseries/offsets/test_dst.py index cbd0aa4a47c9a..9721d7fbd9067 100644 --- a/pandas/tests/tseries/offsets/test_dst.py +++ b/pandas/tests/tseries/offsets/test_dst.py @@ -177,6 +177,9 @@ def test_all_offset_classes(self, tup): 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", [