Skip to content

TST: test_offsets files to better homes & parameterize (#27085) #42991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
CustomBusinessMonthBegin,
CustomBusinessMonthEnd,
DateOffset,
Day,
Easter,
FY5253Quarter,
LastWeekOfMonth,
Expand Down Expand Up @@ -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",
[
Expand Down
61 changes: 60 additions & 1 deletion pandas/tests/tseries/offsets/test_week.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,6 +13,7 @@

from pandas._libs.tslibs import Timestamp
from pandas._libs.tslibs.offsets import (
Day,
LastWeekOfMonth,
Week,
WeekOfMonth,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>"
Expand Down