|
1 | 1 | """
|
2 |
| -Tests for offset.Week, offset.WeekofMonth and offset.LastWeekofMonth |
| 2 | +Tests for the following offsets: |
| 3 | +- Week |
| 4 | +- WeekOfMonth |
| 5 | +- LastWeekOfMonth |
3 | 6 | """
|
4 | 7 | from datetime import (
|
5 | 8 | datetime,
|
|
10 | 13 |
|
11 | 14 | from pandas._libs.tslibs import Timestamp
|
12 | 15 | from pandas._libs.tslibs.offsets import (
|
| 16 | + Day, |
13 | 17 | LastWeekOfMonth,
|
14 | 18 | Week,
|
15 | 19 | WeekOfMonth,
|
@@ -121,6 +125,30 @@ def test_is_on_offset(self, weekday):
|
121 | 125 | expected = False
|
122 | 126 | assert_is_on_offset(offset, date, expected)
|
123 | 127 |
|
| 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 | + |
124 | 152 |
|
125 | 153 | class TestWeekOfMonth(Base):
|
126 | 154 | _offset = WeekOfMonth
|
@@ -221,6 +249,22 @@ def test_is_on_offset(self, case):
|
221 | 249 | offset = WeekOfMonth(week=week, weekday=weekday)
|
222 | 250 | assert offset.is_on_offset(dt) == expected
|
223 | 251 |
|
| 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 | + |
224 | 268 |
|
225 | 269 | class TestLastWeekOfMonth(Base):
|
226 | 270 | _offset = LastWeekOfMonth
|
@@ -298,6 +342,21 @@ def test_is_on_offset(self, case):
|
298 | 342 | offset = LastWeekOfMonth(weekday=weekday)
|
299 | 343 | assert offset.is_on_offset(dt) == expected
|
300 | 344 |
|
| 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 | + |
301 | 360 | def test_repr(self):
|
302 | 361 | assert (
|
303 | 362 | repr(LastWeekOfMonth(n=2, weekday=1)) == "<2 * LastWeekOfMonths: weekday=1>"
|
|
0 commit comments