Skip to content

BUG: #29049 make holiday support offsets of offsets #57938

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
Show file tree
Hide file tree
Changes from 13 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
18 changes: 18 additions & 0 deletions pandas/tests/tseries/holiday/test_holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ def test_both_offset_observance_raises():
)


def test_list_of_list_of_offsets_raises():
# see gh-29049
# Test that the offsets of offsets are forbidden
holiday1 = Holiday(
"Holiday1",
month=USThanksgivingDay.month,
day=USThanksgivingDay.day,
offset=[USThanksgivingDay.offset, DateOffset(1)],
)
with pytest.raises(ValueError, match="Nested lists are not supported for offset"):
Holiday(
"Holiday2",
month=holiday1.month,
day=holiday1.day,
offset=[holiday1.offset, DateOffset(3)],
)


def test_half_open_interval_with_observance():
# Prompted by GH 49075
# Check for holidays that have a half-open date interval where
Expand Down
41 changes: 32 additions & 9 deletions pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
datetime,
timedelta,
)
from typing import (
TYPE_CHECKING,
Callable,
)
import warnings

from dateutil.relativedelta import (
Expand Down Expand Up @@ -33,6 +37,9 @@
Easter,
)

if TYPE_CHECKING:
from pandas._libs.tslibs.offsets import BaseOffset


def next_monday(dt: datetime) -> datetime:
"""
Expand Down Expand Up @@ -149,6 +156,7 @@ class Holiday:
for observance.
"""

offset: BaseOffset | list[BaseOffset] | None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't mypy be able to infer this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. I removed this part.

start_date: Timestamp | None
end_date: Timestamp | None
days_of_week: tuple[int, ...] | None
Expand All @@ -159,24 +167,34 @@ def __init__(
year=None,
month=None,
day=None,
offset=None,
observance=None,
offset: BaseOffset | list[BaseOffset] | None = None,
observance: Callable | None = None,
start_date=None,
end_date=None,
days_of_week=None,
days_of_week: tuple | None = None,
) -> None:
"""
Parameters
----------
name : str
Name of the holiday , defaults to class name
year : int, default None
Year of the holiday
month : int, default None
Month of the holiday
day : int, default None
Day of the holiday
offset : array of pandas.tseries.offsets or
class from pandas.tseries.offsets
computes offset from date
observance: function
computes when holiday is given a pandas Timestamp
days_of_week:
provide a tuple of days e.g (0,1,2,3,) for Monday Through Thursday
class from pandas.tseries.offsets, default None
Computes offset from date
observance : function, default None
Computes when holiday is given a pandas Timestamp
start_date : datetime-like, default None
First date the holiday is observed
end_date : datetime-like, default None
Last date the holiday is observed
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

Examples
Expand Down Expand Up @@ -218,6 +236,11 @@ class from pandas.tseries.offsets
"""
if offset is not None and observance is not None:
raise NotImplementedError("Cannot use both offset and observance.")
if isinstance(offset, list) and any(isinstance(off, list) for off in offset):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if offset is not None and observance is not None:
raise NotImplementedError("Cannot use both offset and observance.")
if isinstance(offset, list) and any(isinstance(off, list) for off in offset):
if offset is not None:
if observance is not None:
raise NotImplementedError("Cannot use both offset and observance.")
if not (isinstance(offset, DateOffset) or (isinstance(list, DateOffset) and all(isinstance(off, DateOffset) for off in offsets))):

raise ValueError(
"Nested lists are not supported for offset. "
"Flatten composite offsets of `Holiday.offset`s first."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just say what is allowed for offset instead of a specific failure case

)

self.name = name
self.year = year
Expand Down