Skip to content

Commit 2400acb

Browse files
committed
TST: test_offsets files to better homes & parameterize (#27045)
1 parent 5bd9bac commit 2400acb

File tree

2 files changed

+60
-61
lines changed

2 files changed

+60
-61
lines changed

pandas/tests/tseries/offsets/test_offsets.py

-60
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
CustomBusinessMonthBegin,
5252
CustomBusinessMonthEnd,
5353
DateOffset,
54-
Day,
5554
Easter,
5655
FY5253Quarter,
5756
LastWeekOfMonth,
@@ -798,65 +797,6 @@ def test_tick_normalize_raises(tick_classes):
798797
cls(n=3, normalize=True)
799798

800799

801-
def test_weeks_onoffset():
802-
# GH#18510 Week with weekday = None, normalize = False should always
803-
# be is_on_offset
804-
offset = Week(n=2, weekday=None)
805-
ts = Timestamp("1862-01-13 09:03:34.873477378+0210", tz="Africa/Lusaka")
806-
fast = offset.is_on_offset(ts)
807-
slow = (ts + offset) - offset == ts
808-
assert fast == slow
809-
810-
# negative n
811-
offset = Week(n=2, weekday=None)
812-
ts = Timestamp("1856-10-24 16:18:36.556360110-0717", tz="Pacific/Easter")
813-
fast = offset.is_on_offset(ts)
814-
slow = (ts + offset) - offset == ts
815-
assert fast == slow
816-
817-
818-
def test_weekofmonth_onoffset():
819-
# GH#18864
820-
# Make sure that nanoseconds don't trip up is_on_offset (and with it apply)
821-
offset = WeekOfMonth(n=2, week=2, weekday=0)
822-
ts = Timestamp("1916-05-15 01:14:49.583410462+0422", tz="Asia/Qyzylorda")
823-
fast = offset.is_on_offset(ts)
824-
slow = (ts + offset) - offset == ts
825-
assert fast == slow
826-
827-
# negative n
828-
offset = WeekOfMonth(n=-3, week=1, weekday=0)
829-
ts = Timestamp("1980-12-08 03:38:52.878321185+0500", tz="Asia/Oral")
830-
fast = offset.is_on_offset(ts)
831-
slow = (ts + offset) - offset == ts
832-
assert fast == slow
833-
834-
835-
def test_last_week_of_month_on_offset():
836-
# GH#19036, GH#18977 _adjust_dst was incorrect for LastWeekOfMonth
837-
offset = LastWeekOfMonth(n=4, weekday=6)
838-
ts = Timestamp("1917-05-27 20:55:27.084284178+0200", tz="Europe/Warsaw")
839-
slow = (ts + offset) - offset == ts
840-
fast = offset.is_on_offset(ts)
841-
assert fast == slow
842-
843-
# negative n
844-
offset = LastWeekOfMonth(n=-4, weekday=5)
845-
ts = Timestamp("2005-08-27 05:01:42.799392561-0500", tz="America/Rainy_River")
846-
slow = (ts + offset) - offset == ts
847-
fast = offset.is_on_offset(ts)
848-
assert fast == slow
849-
850-
851-
def test_week_add_invalid():
852-
# Week with weekday should raise TypeError and _not_ AttributeError
853-
# when adding invalid offset
854-
offset = Week(weekday=1)
855-
other = Day()
856-
with pytest.raises(TypeError, match="Cannot add"):
857-
offset + other
858-
859-
860800
@pytest.mark.parametrize(
861801
"attribute",
862802
[

pandas/tests/tseries/offsets/test_week.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""
2-
Tests for offset.Week, offset.WeekofMonth and offset.LastWeekofMonth
2+
Tests for the following offsets:
3+
- Week
4+
- WeekOfMonth
5+
- LastWeekOfMonth
36
"""
47
from datetime import (
58
datetime,
@@ -10,6 +13,7 @@
1013

1114
from pandas._libs.tslibs import Timestamp
1215
from pandas._libs.tslibs.offsets import (
16+
Day,
1317
LastWeekOfMonth,
1418
Week,
1519
WeekOfMonth,
@@ -121,6 +125,30 @@ def test_is_on_offset(self, weekday):
121125
expected = False
122126
assert_is_on_offset(offset, date, expected)
123127

128+
@pytest.mark.parametrize(
129+
"n,date",
130+
[
131+
(2, "1862-01-13 09:03:34.873477378+0210"),
132+
(-2, "1856-10-24 16:18:36.556360110-0717"),
133+
],
134+
)
135+
def test_is_on_offset_weekday_none(self, n, date):
136+
# GH 18510 Week with weekday = None, normalize = False
137+
# should always be is_on_offset
138+
offset = Week(n=n, weekday=None)
139+
ts = Timestamp(date, tz="Africa/Lusaka")
140+
fast = offset.is_on_offset(ts)
141+
slow = (ts + offset) - offset == ts
142+
assert fast == slow
143+
144+
def test_week_add_invalid(self):
145+
# Week with weekday should raise TypeError and _not_ AttributeError
146+
# when adding invalid offset
147+
offset = Week(weekday=1)
148+
other = Day()
149+
with pytest.raises(TypeError, match="Cannot add"):
150+
offset + other
151+
124152

125153
class TestWeekOfMonth(Base):
126154
_offset = WeekOfMonth
@@ -221,6 +249,22 @@ def test_is_on_offset(self, case):
221249
offset = WeekOfMonth(week=week, weekday=weekday)
222250
assert offset.is_on_offset(dt) == expected
223251

252+
@pytest.mark.parametrize(
253+
"n,week,date,tz",
254+
[
255+
(2, 2, "1916-05-15 01:14:49.583410462+0422", "Asia/Qyzylorda"),
256+
(-3, 1, "1980-12-08 03:38:52.878321185+0500", "Asia/Oral"),
257+
],
258+
)
259+
def test_is_on_offset_nanoseconds(self, n, week, date, tz):
260+
# GH 18864
261+
# Make sure that nanoseconds don't trip up is_on_offset (and with it apply)
262+
offset = WeekOfMonth(n=n, week=week, weekday=0)
263+
ts = Timestamp(date, tz=tz)
264+
fast = offset.is_on_offset(ts)
265+
slow = (ts + offset) - offset == ts
266+
assert fast == slow
267+
224268

225269
class TestLastWeekOfMonth(Base):
226270
_offset = LastWeekOfMonth
@@ -298,6 +342,21 @@ def test_is_on_offset(self, case):
298342
offset = LastWeekOfMonth(weekday=weekday)
299343
assert offset.is_on_offset(dt) == expected
300344

345+
@pytest.mark.parametrize(
346+
"n,weekday,date,tz",
347+
[
348+
(4, 6, "1917-05-27 20:55:27.084284178+0200", "Europe/Warsaw"),
349+
(-4, 5, "2005-08-27 05:01:42.799392561-0500", "America/Rainy_River"),
350+
],
351+
)
352+
def test_last_week_of_month_on_offset(self, n, weekday, date, tz):
353+
# GH 19036, GH 18977 _adjust_dst was incorrect for LastWeekOfMonth
354+
offset = LastWeekOfMonth(n=n, weekday=weekday)
355+
ts = Timestamp(date, tz=tz)
356+
slow = (ts + offset) - offset == ts
357+
fast = offset.is_on_offset(ts)
358+
assert fast == slow
359+
301360
def test_repr(self):
302361
assert (
303362
repr(LastWeekOfMonth(n=2, weekday=1)) == "<2 * LastWeekOfMonths: weekday=1>"

0 commit comments

Comments
 (0)