From 956d7ee0dc4b8f3f9a9a13d7310dfba0d17a6fdb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 18 May 2020 20:43:21 -0700 Subject: [PATCH 1/2] REF: make BusinessMixin a cdef class --- pandas/_libs/tslibs/offsets.pyx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 17ea389611b84..3d0f1e55cb8de 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -973,13 +973,17 @@ def delta_to_tick(delta: timedelta) -> Tick: # -------------------------------------------------------------------- -class BusinessMixin(BaseOffset): +cdef class BusinessMixin(BaseOffset): """ Mixin to business types to provide related functions. """ + + cdef readonly: + timedelta _offset + def __init__(self, n=1, normalize=False, offset=timedelta(0)): BaseOffset.__init__(self, n, normalize) - object.__setattr__(self, "_offset", offset) + self._offset = offset @property def offset(self): From 5bd9e0a86da648937369ef7badfb3c426525d3a2 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 20 May 2020 20:22:46 -0700 Subject: [PATCH 2/2] REF: Make BusinessMixin a cdef class --- pandas/_libs/tslibs/offsets.pyx | 8 ++++++++ pandas/tseries/offsets.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 1c57b00de2690..8b00b6edba66b 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -1011,6 +1011,11 @@ cdef class BusinessMixin(BaseOffset): out += ": " + ", ".join(attrs) return out + cpdef __setstate__(self, state): + # We need to use a cdef/cpdef method to set the readonly _offset attribute + BaseOffset.__setstate__(self, state) + self._offset = state["_offset"] + class BusinessHourMixin(BusinessMixin): _adjust_dst = False @@ -1064,6 +1069,9 @@ class BusinessHourMixin(BusinessMixin): object.__setattr__(self, "start", start) object.__setattr__(self, "end", end) + def __reduce__(self): + return type(self), (self.n, self.normalize, self.start, self.end, self.offset) + def _repr_attrs(self) -> str: out = super()._repr_attrs() hours = ",".join( diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index 8aced50b78ae2..cbc707d4440a0 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -919,6 +919,13 @@ def __init__( BusinessMixin.__init__(self, n, normalize, offset) CustomMixin.__init__(self, weekmask, holidays, calendar) + def __reduce__(self): + # None for self.calendar bc np.busdaycalendar doesnt pickle nicely + return ( + type(self), + (self.n, self.normalize, self.weekmask, self.holidays, None, self.offset), + ) + @cache_readonly def cbday_roll(self): """