diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index 08dbc1345b9d4..dfa2a7a67b877 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -51,7 +51,6 @@ CustomBusinessMonthBegin, CustomBusinessMonthEnd, DateOffset, - Day, Easter, FY5253Quarter, LastWeekOfMonth, @@ -798,65 +797,6 @@ def test_tick_normalize_raises(tick_classes): cls(n=3, normalize=True) -def test_weeks_onoffset(): - # GH#18510 Week with weekday = None, normalize = False should always - # be is_on_offset - offset = Week(n=2, weekday=None) - ts = Timestamp("1862-01-13 09:03:34.873477378+0210", tz="Africa/Lusaka") - fast = offset.is_on_offset(ts) - slow = (ts + offset) - offset == ts - assert fast == slow - - # negative n - offset = Week(n=2, weekday=None) - ts = Timestamp("1856-10-24 16:18:36.556360110-0717", tz="Pacific/Easter") - fast = offset.is_on_offset(ts) - slow = (ts + offset) - offset == ts - assert fast == slow - - -def test_weekofmonth_onoffset(): - # GH#18864 - # Make sure that nanoseconds don't trip up is_on_offset (and with it apply) - offset = WeekOfMonth(n=2, week=2, weekday=0) - ts = Timestamp("1916-05-15 01:14:49.583410462+0422", tz="Asia/Qyzylorda") - fast = offset.is_on_offset(ts) - slow = (ts + offset) - offset == ts - assert fast == slow - - # negative n - offset = WeekOfMonth(n=-3, week=1, weekday=0) - ts = Timestamp("1980-12-08 03:38:52.878321185+0500", tz="Asia/Oral") - fast = offset.is_on_offset(ts) - slow = (ts + offset) - offset == ts - assert fast == slow - - -def test_last_week_of_month_on_offset(): - # GH#19036, GH#18977 _adjust_dst was incorrect for LastWeekOfMonth - offset = LastWeekOfMonth(n=4, weekday=6) - ts = Timestamp("1917-05-27 20:55:27.084284178+0200", tz="Europe/Warsaw") - slow = (ts + offset) - offset == ts - fast = offset.is_on_offset(ts) - assert fast == slow - - # negative n - offset = LastWeekOfMonth(n=-4, weekday=5) - ts = Timestamp("2005-08-27 05:01:42.799392561-0500", tz="America/Rainy_River") - slow = (ts + offset) - offset == ts - fast = offset.is_on_offset(ts) - assert fast == slow - - -def test_week_add_invalid(): - # Week with weekday should raise TypeError and _not_ AttributeError - # when adding invalid offset - offset = Week(weekday=1) - other = Day() - with pytest.raises(TypeError, match="Cannot add"): - offset + other - - @pytest.mark.parametrize( "attribute", [ diff --git a/pandas/tests/tseries/offsets/test_week.py b/pandas/tests/tseries/offsets/test_week.py index b46a36e00f2da..be574fd963eff 100644 --- a/pandas/tests/tseries/offsets/test_week.py +++ b/pandas/tests/tseries/offsets/test_week.py @@ -1,5 +1,8 @@ """ -Tests for offset.Week, offset.WeekofMonth and offset.LastWeekofMonth +Tests for the following offsets: +- Week +- WeekOfMonth +- LastWeekOfMonth """ from datetime import ( datetime, @@ -10,6 +13,7 @@ from pandas._libs.tslibs import Timestamp from pandas._libs.tslibs.offsets import ( + Day, LastWeekOfMonth, Week, WeekOfMonth, @@ -121,6 +125,30 @@ def test_is_on_offset(self, weekday): expected = False assert_is_on_offset(offset, date, expected) + @pytest.mark.parametrize( + "n,date", + [ + (2, "1862-01-13 09:03:34.873477378+0210"), + (-2, "1856-10-24 16:18:36.556360110-0717"), + ], + ) + def test_is_on_offset_weekday_none(self, n, date): + # GH 18510 Week with weekday = None, normalize = False + # should always be is_on_offset + offset = Week(n=n, weekday=None) + ts = Timestamp(date, tz="Africa/Lusaka") + fast = offset.is_on_offset(ts) + slow = (ts + offset) - offset == ts + assert fast == slow + + def test_week_add_invalid(self): + # Week with weekday should raise TypeError and _not_ AttributeError + # when adding invalid offset + offset = Week(weekday=1) + other = Day() + with pytest.raises(TypeError, match="Cannot add"): + offset + other + class TestWeekOfMonth(Base): _offset = WeekOfMonth @@ -221,6 +249,22 @@ def test_is_on_offset(self, case): offset = WeekOfMonth(week=week, weekday=weekday) assert offset.is_on_offset(dt) == expected + @pytest.mark.parametrize( + "n,week,date,tz", + [ + (2, 2, "1916-05-15 01:14:49.583410462+0422", "Asia/Qyzylorda"), + (-3, 1, "1980-12-08 03:38:52.878321185+0500", "Asia/Oral"), + ], + ) + def test_is_on_offset_nanoseconds(self, n, week, date, tz): + # GH 18864 + # Make sure that nanoseconds don't trip up is_on_offset (and with it apply) + offset = WeekOfMonth(n=n, week=week, weekday=0) + ts = Timestamp(date, tz=tz) + fast = offset.is_on_offset(ts) + slow = (ts + offset) - offset == ts + assert fast == slow + class TestLastWeekOfMonth(Base): _offset = LastWeekOfMonth @@ -298,6 +342,21 @@ def test_is_on_offset(self, case): offset = LastWeekOfMonth(weekday=weekday) assert offset.is_on_offset(dt) == expected + @pytest.mark.parametrize( + "n,weekday,date,tz", + [ + (4, 6, "1917-05-27 20:55:27.084284178+0200", "Europe/Warsaw"), + (-4, 5, "2005-08-27 05:01:42.799392561-0500", "America/Rainy_River"), + ], + ) + def test_last_week_of_month_on_offset(self, n, weekday, date, tz): + # GH 19036, GH 18977 _adjust_dst was incorrect for LastWeekOfMonth + offset = LastWeekOfMonth(n=n, weekday=weekday) + ts = Timestamp(date, tz=tz) + slow = (ts + offset) - offset == ts + fast = offset.is_on_offset(ts) + assert fast == slow + def test_repr(self): assert ( repr(LastWeekOfMonth(n=2, weekday=1)) == "<2 * LastWeekOfMonths: weekday=1>"