Skip to content

Commit d7b1661

Browse files
jrebackjorisvandenbossche
authored andcommitted
[Backport pandas-dev#14685] BUG: fix pickling of Custom offsets in 3.6
xref pandas-dev#14679 Author: Jeff Reback <[email protected]> Closes pandas-dev#14685 from jreback/offsets and squashes the following commits: 8ad212c [Jeff Reback] BUG: fix pickling of Custom offsets in 3.6 (cherry picked from commit bec5bdb)
1 parent 332df4a commit d7b1661

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

pandas/tseries/offsets.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,32 @@ def _repr_attrs(self):
553553
out += ': ' + ', '.join(attrs)
554554
return out
555555

556+
def __getstate__(self):
557+
"""Return a pickleable state"""
558+
state = self.__dict__.copy()
559+
560+
# we don't want to actually pickle the calendar object
561+
# as its a np.busyday; we recreate on deserilization
562+
if 'calendar' in state:
563+
del state['calendar']
564+
try:
565+
state['kwds'].pop('calendar')
566+
except KeyError:
567+
pass
568+
569+
return state
570+
571+
def __setstate__(self, state):
572+
"""Reconstruct an instance from a pickled state"""
573+
self.__dict__ = state
574+
if 'weekmask' in state and 'holidays' in state:
575+
calendar, holidays = self.get_calendar(weekmask=self.weekmask,
576+
holidays=self.holidays,
577+
calendar=None)
578+
self.kwds['calendar'] = self.calendar = calendar
579+
self.kwds['holidays'] = self.holidays = holidays
580+
self.kwds['weekmask'] = state['weekmask']
581+
556582

557583
class BusinessDay(BusinessMixin, SingleConstructorOffset):
558584
"""
@@ -992,30 +1018,6 @@ def get_calendar(self, weekmask, holidays, calendar):
9921018
busdaycalendar = np.busdaycalendar(**kwargs)
9931019
return busdaycalendar, holidays
9941020

995-
def __getstate__(self):
996-
"""Return a pickleable state"""
997-
state = self.__dict__.copy()
998-
del state['calendar']
999-
1000-
# we don't want to actually pickle the calendar object
1001-
# as its a np.busyday; we recreate on deserilization
1002-
try:
1003-
state['kwds'].pop('calendar')
1004-
except:
1005-
pass
1006-
1007-
return state
1008-
1009-
def __setstate__(self, state):
1010-
"""Reconstruct an instance from a pickled state"""
1011-
self.__dict__ = state
1012-
calendar, holidays = self.get_calendar(weekmask=self.weekmask,
1013-
holidays=self.holidays,
1014-
calendar=None)
1015-
self.kwds['calendar'] = self.calendar = calendar
1016-
self.kwds['holidays'] = self.holidays = holidays
1017-
self.kwds['weekmask'] = state['weekmask']
1018-
10191021
@apply_wraps
10201022
def apply(self, other):
10211023
if self.n <= 0:

0 commit comments

Comments
 (0)