Skip to content

Commit 55ad334

Browse files
authored
REF: make BusinessMixin a cdef class (#34285)
* REF: make BusinessMixin a cdef class * REF: Make BusinessMixin a cdef class
1 parent 89f8af7 commit 55ad334

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pandas/_libs/tslibs/offsets.pyx

+14-2
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,17 @@ def delta_to_tick(delta: timedelta) -> Tick:
988988
# --------------------------------------------------------------------
989989

990990

991-
class BusinessMixin(SingleConstructorOffset):
991+
cdef class BusinessMixin(SingleConstructorOffset):
992992
"""
993993
Mixin to business types to provide related functions.
994994
"""
995+
996+
cdef readonly:
997+
timedelta _offset
998+
995999
def __init__(self, n=1, normalize=False, offset=timedelta(0)):
9961000
BaseOffset.__init__(self, n, normalize)
997-
object.__setattr__(self, "_offset", offset)
1001+
self._offset = offset
9981002

9991003
@property
10001004
def offset(self):
@@ -1014,6 +1018,11 @@ class BusinessMixin(SingleConstructorOffset):
10141018
out += ": " + ", ".join(attrs)
10151019
return out
10161020

1021+
cpdef __setstate__(self, state):
1022+
# We need to use a cdef/cpdef method to set the readonly _offset attribute
1023+
BaseOffset.__setstate__(self, state)
1024+
self._offset = state["_offset"]
1025+
10171026

10181027
class BusinessHourMixin(BusinessMixin):
10191028
_adjust_dst = False
@@ -1067,6 +1076,9 @@ class BusinessHourMixin(BusinessMixin):
10671076
object.__setattr__(self, "start", start)
10681077
object.__setattr__(self, "end", end)
10691078

1079+
def __reduce__(self):
1080+
return type(self), (self.n, self.normalize, self.start, self.end, self.offset)
1081+
10701082
def _repr_attrs(self) -> str:
10711083
out = super()._repr_attrs()
10721084
hours = ",".join(

pandas/tseries/offsets.py

+7
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,13 @@ def __init__(
869869
BusinessMixin.__init__(self, n, normalize, offset)
870870
CustomMixin.__init__(self, weekmask, holidays, calendar)
871871

872+
def __reduce__(self):
873+
# None for self.calendar bc np.busdaycalendar doesnt pickle nicely
874+
return (
875+
type(self),
876+
(self.n, self.normalize, self.weekmask, self.holidays, None, self.offset),
877+
)
878+
872879
@cache_readonly
873880
def cbday_roll(self):
874881
"""

0 commit comments

Comments
 (0)