Skip to content

Commit 556a6af

Browse files
authored
REF: make CustomMixin a cdef class (#34345)
1 parent 70d7c04 commit 556a6af

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

doc/source/reference/offset_frequency.rst

+31
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Properties
5959
BusinessDay.normalize
6060
BusinessDay.rule_code
6161
BusinessDay.n
62+
BusinessDay.weekmask
63+
BusinessDay.holidays
64+
BusinessDay.calendar
6265

6366
Methods
6467
~~~~~~~
@@ -95,6 +98,9 @@ Properties
9598
BusinessHour.n
9699
BusinessHour.start
97100
BusinessHour.end
101+
BusinessHour.weekmask
102+
BusinessHour.holidays
103+
BusinessHour.calendar
98104

99105
Methods
100106
~~~~~~~
@@ -128,6 +134,9 @@ Properties
128134
CustomBusinessDay.normalize
129135
CustomBusinessDay.rule_code
130136
CustomBusinessDay.n
137+
CustomBusinessDay.weekmask
138+
CustomBusinessDay.calendar
139+
CustomBusinessDay.holidays
131140

132141
Methods
133142
~~~~~~~
@@ -162,6 +171,9 @@ Properties
162171
CustomBusinessHour.normalize
163172
CustomBusinessHour.rule_code
164173
CustomBusinessHour.n
174+
CustomBusinessHour.weekmask
175+
CustomBusinessHour.calendar
176+
CustomBusinessHour.holidays
165177
CustomBusinessHour.start
166178
CustomBusinessHour.end
167179

@@ -334,6 +346,9 @@ Properties
334346
CustomBusinessMonthEnd.normalize
335347
CustomBusinessMonthEnd.rule_code
336348
CustomBusinessMonthEnd.n
349+
CustomBusinessMonthEnd.weekmask
350+
CustomBusinessMonthEnd.calendar
351+
CustomBusinessMonthEnd.holidays
337352

338353
Methods
339354
~~~~~~~
@@ -368,6 +383,9 @@ Properties
368383
CustomBusinessMonthBegin.normalize
369384
CustomBusinessMonthBegin.rule_code
370385
CustomBusinessMonthBegin.n
386+
CustomBusinessMonthBegin.weekmask
387+
CustomBusinessMonthBegin.calendar
388+
CustomBusinessMonthBegin.holidays
371389

372390
Methods
373391
~~~~~~~
@@ -1241,6 +1259,9 @@ Properties
12411259
BDay.offset
12421260
BDay.rule_code
12431261
BDay.n
1262+
BDay.weekmask
1263+
BDay.holidays
1264+
BDay.calendar
12441265

12451266
Methods
12461267
~~~~~~~
@@ -1356,6 +1377,9 @@ Properties
13561377
CBMonthEnd.offset
13571378
CBMonthEnd.rule_code
13581379
CBMonthEnd.n
1380+
CBMonthEnd.weekmask
1381+
CBMonthEnd.holidays
1382+
CBMonthEnd.calendar
13591383

13601384
Methods
13611385
~~~~~~~
@@ -1397,6 +1421,9 @@ Properties
13971421
CBMonthBegin.offset
13981422
CBMonthBegin.rule_code
13991423
CBMonthBegin.n
1424+
CBMonthBegin.weekmask
1425+
CBMonthBegin.holidays
1426+
CBMonthBegin.calendar
14001427

14011428
Methods
14021429
~~~~~~~
@@ -1435,6 +1462,9 @@ Properties
14351462
CDay.offset
14361463
CDay.rule_code
14371464
CDay.n
1465+
CDay.weekmask
1466+
CDay.calendar
1467+
CDay.holidays
14381468

14391469
Methods
14401470
~~~~~~~
@@ -1452,6 +1482,7 @@ Methods
14521482
CDay.rollforward
14531483
CDay.__call__
14541484

1485+
14551486
.. _api.frequencies:
14561487

14571488
===========

pandas/_libs/tslibs/offsets.pyx

+36-27
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,27 @@ cdef class BusinessMixin(SingleConstructorOffset):
12471247

12481248
cdef readonly:
12491249
timedelta _offset
1250+
# Only Custom subclasses use weekmask, holiday, calendar
1251+
object weekmask, holidays, calendar
12501252

12511253
def __init__(self, n=1, normalize=False, offset=timedelta(0)):
12521254
BaseOffset.__init__(self, n, normalize)
12531255
self._offset = offset
12541256

1257+
cpdef _init_custom(self, weekmask, holidays, calendar):
1258+
"""
1259+
Additional __init__ for Custom subclasses.
1260+
"""
1261+
calendar, holidays = _get_calendar(
1262+
weekmask=weekmask, holidays=holidays, calendar=calendar
1263+
)
1264+
# Custom offset instances are identified by the
1265+
# following two attributes. See DateOffset._params()
1266+
# holidays, weekmask
1267+
self.weekmask = weekmask
1268+
self.holidays = holidays
1269+
self.calendar = calendar
1270+
12551271
@property
12561272
def offset(self):
12571273
"""
@@ -1276,6 +1292,18 @@ cdef class BusinessMixin(SingleConstructorOffset):
12761292
self._offset = state.pop("_offset")
12771293
elif "offset" in state:
12781294
self._offset = state.pop("offset")
1295+
1296+
if self._prefix.startswith("C"):
1297+
# i.e. this is a Custom class
1298+
weekmask = state.pop("weekmask")
1299+
holidays = state.pop("holidays")
1300+
calendar, holidays = _get_calendar(weekmask=weekmask,
1301+
holidays=holidays,
1302+
calendar=None)
1303+
self.weekmask = weekmask
1304+
self.calendar = calendar
1305+
self.holidays = holidays
1306+
12791307
BaseOffset.__setstate__(self, state)
12801308

12811309

@@ -1785,25 +1813,6 @@ cdef class BusinessHour(BusinessMixin):
17851813
return False
17861814

17871815

1788-
class CustomMixin:
1789-
"""
1790-
Mixin for classes that define and validate calendar, holidays,
1791-
and weekdays attributes.
1792-
"""
1793-
1794-
def __init__(self, weekmask, holidays, calendar):
1795-
calendar, holidays = _get_calendar(
1796-
weekmask=weekmask, holidays=holidays, calendar=calendar
1797-
)
1798-
# Custom offset instances are identified by the
1799-
# following two attributes. See DateOffset._params()
1800-
# holidays, weekmask
1801-
1802-
object.__setattr__(self, "weekmask", weekmask)
1803-
object.__setattr__(self, "holidays", holidays)
1804-
object.__setattr__(self, "calendar", calendar)
1805-
1806-
18071816
cdef class WeekOfMonthMixin(SingleConstructorOffset):
18081817
"""
18091818
Mixin for methods common to WeekOfMonth and LastWeekOfMonth.
@@ -3252,7 +3261,7 @@ cdef class Easter(SingleConstructorOffset):
32523261
# Custom Offset classes
32533262

32543263

3255-
class CustomBusinessDay(CustomMixin, BusinessDay):
3264+
cdef class CustomBusinessDay(BusinessDay):
32563265
"""
32573266
DateOffset subclass representing custom business days excluding holidays.
32583267
@@ -3291,12 +3300,12 @@ class CustomBusinessDay(CustomMixin, BusinessDay):
32913300
offset=timedelta(0),
32923301
):
32933302
BusinessDay.__init__(self, n, normalize, offset)
3294-
CustomMixin.__init__(self, weekmask, holidays, calendar)
3303+
self._init_custom(weekmask, holidays, calendar)
32953304

3296-
def __setstate__(self, state):
3305+
cpdef __setstate__(self, state):
32973306
self.holidays = state.pop("holidays")
32983307
self.weekmask = state.pop("weekmask")
3299-
super().__setstate__(state)
3308+
BusinessDay.__setstate__(self, state)
33003309

33013310
@apply_wraps
33023311
def apply(self, other):
@@ -3338,7 +3347,7 @@ class CustomBusinessDay(CustomMixin, BusinessDay):
33383347
return np.is_busday(day64, busdaycal=self.calendar)
33393348

33403349

3341-
class CustomBusinessHour(CustomMixin, BusinessHour):
3350+
class CustomBusinessHour(BusinessHour):
33423351
"""
33433352
DateOffset subclass representing possibly n custom business days.
33443353
"""
@@ -3361,7 +3370,7 @@ class CustomBusinessHour(CustomMixin, BusinessHour):
33613370
offset=timedelta(0),
33623371
):
33633372
BusinessHour.__init__(self, n, normalize, start=start, end=end, offset=offset)
3364-
CustomMixin.__init__(self, weekmask, holidays, calendar)
3373+
self._init_custom(weekmask, holidays, calendar)
33653374

33663375
def __reduce__(self):
33673376
# None for self.calendar bc np.busdaycalendar doesnt pickle nicely
@@ -3380,7 +3389,7 @@ class CustomBusinessHour(CustomMixin, BusinessHour):
33803389
)
33813390

33823391

3383-
class _CustomBusinessMonth(CustomMixin, BusinessMixin, MonthOffset):
3392+
class _CustomBusinessMonth(BusinessMixin, MonthOffset):
33843393
"""
33853394
DateOffset subclass representing custom business month(s).
33863395
@@ -3420,7 +3429,7 @@ class _CustomBusinessMonth(CustomMixin, BusinessMixin, MonthOffset):
34203429
offset=timedelta(0),
34213430
):
34223431
BusinessMixin.__init__(self, n, normalize, offset)
3423-
CustomMixin.__init__(self, weekmask, holidays, calendar)
3432+
self._init_custom(weekmask, holidays, calendar)
34243433

34253434
def __reduce__(self):
34263435
# None for self.calendar bc np.busdaycalendar doesnt pickle nicely

0 commit comments

Comments
 (0)