Skip to content

Commit d02912d

Browse files
committed
BUG: make CustomBusinessDay respect provided calendar; require numpy.busdaycalendar (#60647)
1 parent c4449f0 commit d02912d

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ Datetimelike
972972
- Bug in constructing arrays with :class:`ArrowDtype` with ``timestamp`` type incorrectly allowing ``Decimal("NaN")`` (:issue:`61773`)
973973
- Bug in constructing arrays with a timezone-aware :class:`ArrowDtype` from timezone-naive datetime objects incorrectly treating those as UTC times instead of wall times like :class:`DatetimeTZDtype` (:issue:`61775`)
974974
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)
975+
- Bug in :class:`CustomBusinessDay` where calendar parameter validation was not enforced, now requires ``numpy.busdaycalendar`` objects (:issue:`60647`)
975976

976977

977978
Timedelta

pandas/_libs/tslibs/offsets.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ cdef _get_calendar(weekmask, holidays, calendar):
216216
pass
217217
return calendar, holidays
218218

219+
# GH#60647
220+
# Enforce that when a calendar is provided, it must be a numpy.busdaycalendar
221+
if calendar is not None and 'pandas_market_calendars' in str(type(calendar)):
222+
raise TypeError(
223+
"CustomBusinessDay.calendar must be a numpy.busdaycalendar; "
224+
f"got {type(calendar).__name__}. "
225+
"For pandas_market_calendars please pass the result of "
226+
".holidays() (a DatetimeIndex) instead."
227+
)
228+
219229
if holidays is None:
220230
holidays = []
221231
try:

pandas/tests/tseries/offsets/test_custom_business_day.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ def test_calendar(self):
8282
dt = datetime(2014, 1, 17)
8383
assert_offset_equal(CDay(calendar=calendar), dt, datetime(2014, 1, 21))
8484

85+
def test_calendar_validation(self):
86+
"""Test CustomBusinessDay calendar parameter validation (GH#60647)"""
87+
# Valid numpy.busdaycalendar should work
88+
calendar = np.busdaycalendar(weekmask="1111100") # Monday-Friday
89+
cbd = CDay(calendar=calendar)
90+
assert cbd.calendar is calendar
91+
92+
# Test error message for pandas_market_calendars scenario
93+
class pandas_market_calendars:
94+
def __init__(self):
95+
self.name = "pandas_market_calendars"
96+
97+
mock_calendar = pandas_market_calendars()
98+
with pytest.raises(TypeError):
99+
CDay(calendar=mock_calendar)
100+
85101
def test_roundtrip_pickle(self, offset, offset2):
86102
def _check_roundtrip(obj):
87103
unpickled = tm.round_trip_pickle(obj)

0 commit comments

Comments
 (0)