-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DOC/ENH: Holiday exclusion argument #61600
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
Changes from 11 commits
f054afb
5a809e2
891b442
aa08735
f019a5c
bdbb555
5bca8c7
655ca4e
f46c394
27cca3d
a9a745c
7710e43
5cfd599
2d9e4c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,3 +353,71 @@ def test_holidays_with_timezone_specified_but_no_occurences(): | |
expected_results.index = expected_results.index.as_unit("ns") | ||
|
||
tm.assert_equal(test_case, expected_results) | ||
|
||
|
||
def test_holiday_with_exclusion(): | ||
# GH 54382 | ||
start = Timestamp("2020-05-01") | ||
end = Timestamp("2025-05-31") | ||
exclude = DatetimeIndex([Timestamp("2022-05-30")]) # Queen's platinum Jubilee | ||
default_uk_spring_bank_holiday: Holiday = Holiday( | ||
"UK Spring Bank Holiday", | ||
month=5, | ||
day=31, | ||
offset=DateOffset(weekday=MO(-1)), | ||
) | ||
|
||
queens_jubilee_uk_spring_bank_holiday: Holiday = Holiday( | ||
"Queen's Jubilee UK Spring Bank Holiday", | ||
month=5, | ||
day=31, | ||
offset=DateOffset(weekday=MO(-1)), | ||
exclude_dates=exclude, | ||
) | ||
|
||
original_dates = default_uk_spring_bank_holiday.dates(start, end) | ||
actual_dates = queens_jubilee_uk_spring_bank_holiday.dates(start, end) | ||
print(exclude.isin(original_dates).all()) | ||
|
||
assert exclude.isin(original_dates).all() | ||
assert ~exclude.isin(actual_dates).all() | ||
assert actual_dates.isin(original_dates).all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you construct the resulting result = queens_jubilee_uk_spring_bank_holiday.dates(start, end)
expected = DatetimeIndex(...)
tm.assert_index_equal(result, expected) And below as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the tm.assert_index_equal() in addition to the above 3 asserts, hopefully that is okay? I can also delete the 3 assert statements from above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes could you delete the 3 assert statements in this test and below? Hardcoding the expected result and using |
||
|
||
|
||
def test_holiday_with_multiple_exclusions(): | ||
start = Timestamp("2000-01-01") | ||
end = Timestamp("2100-05-31") | ||
exclude = DatetimeIndex( | ||
[ | ||
Timestamp("2025-01-01"), | ||
Timestamp("2042-01-01"), | ||
Timestamp("2061-01-01"), | ||
] | ||
) # Yakudoshi new year | ||
default_japan_new_year: Holiday = Holiday( | ||
"Japan New Year", | ||
month=1, | ||
day=1, | ||
) | ||
|
||
yakudoshi_new_year: Holiday = Holiday( | ||
"Yakudoshi New Year", month=1, day=1, exclude_dates=exclude | ||
) | ||
|
||
original_dates = default_japan_new_year.dates(start, end) | ||
actual_dates = yakudoshi_new_year.dates(start, end) | ||
|
||
assert exclude.isin(original_dates).all() | ||
assert ~exclude.isin(actual_dates).all() | ||
assert actual_dates.isin(original_dates).all() | ||
|
||
|
||
def test_exclude_date_value_error(): | ||
msg = "exclude_dates must be None or of type DatetimeIndex." | ||
|
||
with pytest.raises(ValueError, match=msg): | ||
exclude = [ | ||
Timestamp("2025-06-10"), | ||
Timestamp("2026-06-10"), | ||
] | ||
Holiday("National Ice Tea Day", month=6, day=10, exclude_dates=exclude) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,6 +169,7 @@ def __init__( | |
start_date=None, | ||
end_date=None, | ||
days_of_week: tuple | None = None, | ||
exclude_dates: DatetimeIndex | None = None, | ||
) -> None: | ||
""" | ||
Parameters | ||
|
@@ -193,6 +194,8 @@ class from pandas.tseries.offsets, default None | |
days_of_week : tuple of int or dateutil.relativedelta weekday strs, default None | ||
Provide a tuple of days e.g (0,1,2,3,) for Monday Through Thursday | ||
Monday=0,..,Sunday=6 | ||
exclude_dates : DatetimeIndex or default None | ||
Specific dates to exclude e.g. skipping a specific year's holiday | ||
Examples | ||
-------- | ||
|
@@ -257,6 +260,9 @@ class from pandas.tseries.offsets, default None | |
self.observance = observance | ||
assert days_of_week is None or type(days_of_week) == tuple | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I also switch this to throw a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A separate PR would be better, thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this, should I open a new issue? Or just make another PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just another PR is fine |
||
self.days_of_week = days_of_week | ||
if not (exclude_dates is None or isinstance(exclude_dates, DatetimeIndex)): | ||
raise ValueError("exclude_dates must be None or of type DatetimeIndex.") | ||
mroeschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.exclude_dates = exclude_dates | ||
|
||
def __repr__(self) -> str: | ||
info = "" | ||
|
@@ -328,6 +334,9 @@ def dates( | |
holiday_dates = holiday_dates[ | ||
(holiday_dates >= filter_start_date) & (holiday_dates <= filter_end_date) | ||
] | ||
|
||
if self.exclude_dates is not None: | ||
holiday_dates = holiday_dates.difference(self.exclude_dates) | ||
if return_name: | ||
return Series(self.name, index=holiday_dates) | ||
return holiday_dates | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.