Skip to content

remove daytime attr, move getstate and setstate to base class #21533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,45 @@ class _BaseOffset(object):
'got {n}'.format(n=n))
return nint

def __setstate__(self, state):
"""Reconstruct an instance from a pickled state"""
if 'offset' in state:
# Older (<0.22.0) versions have offset attribute instead of _offset
if '_offset' in state: # pragma: no cover
raise AssertionError('Unexpected key `_offset`')
state['_offset'] = state.pop('offset')
state['kwds']['offset'] = state['_offset']

if '_offset' in state and not isinstance(state['_offset'], timedelta):
# relativedelta, we need to populate using its kwds
offset = state['_offset']
odict = offset.__dict__
kwds = {key: odict[key] for key in odict if odict[key]}
state.update(kwds)

self.__dict__ = state
if 'weekmask' in state and 'holidays' in state:
calendar, holidays = _get_calendar(weekmask=self.weekmask,
holidays=self.holidays,
calendar=None)
self.calendar = calendar
self.holidays = holidays

def __getstate__(self):
"""Return a pickleable state"""
state = self.__dict__.copy()

# we don't want to actually pickle the calendar object
# as its a np.busyday; we recreate on deserilization
if 'calendar' in state:
del state['calendar']
try:
state['kwds'].pop('calendar')
except KeyError:
pass

return state


class BaseOffset(_BaseOffset):
# Here we add __rfoo__ methods that don't play well with cdef classes
Expand Down
40 changes: 0 additions & 40 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,30 +423,6 @@ def _offset_str(self):
def nanos(self):
raise ValueError("{name} is a non-fixed frequency".format(name=self))

def __setstate__(self, state):
"""Reconstruct an instance from a pickled state"""
if 'offset' in state:
# Older (<0.22.0) versions have offset attribute instead of _offset
if '_offset' in state: # pragma: no cover
raise AssertionError('Unexpected key `_offset`')
state['_offset'] = state.pop('offset')
state['kwds']['offset'] = state['_offset']

if '_offset' in state and not isinstance(state['_offset'], timedelta):
# relativedelta, we need to populate using its kwds
offset = state['_offset']
odict = offset.__dict__
kwds = {key: odict[key] for key in odict if odict[key]}
state.update(kwds)

self.__dict__ = state
if 'weekmask' in state and 'holidays' in state:
calendar, holidays = _get_calendar(weekmask=self.weekmask,
holidays=self.holidays,
calendar=None)
self.calendar = calendar
self.holidays = holidays


class SingleConstructorOffset(DateOffset):
@classmethod
Expand Down Expand Up @@ -494,21 +470,6 @@ def _repr_attrs(self):
out += ': ' + ', '.join(attrs)
return out

def __getstate__(self):
"""Return a pickleable state"""
state = self.__dict__.copy()

# we don't want to actually pickle the calendar object
# as its a np.busyday; we recreate on deserilization
if 'calendar' in state:
del state['calendar']
try:
state['kwds'].pop('calendar')
except KeyError:
pass

return state


class BusinessDay(BusinessMixin, SingleConstructorOffset):
"""
Expand Down Expand Up @@ -690,7 +651,6 @@ def _get_business_hours_by_sec(self):
until = datetime(2014, 4, 1, self.end.hour, self.end.minute)
return (until - dtstart).total_seconds()
else:
self.daytime = False
dtstart = datetime(2014, 4, 1, self.start.hour, self.start.minute)
until = datetime(2014, 4, 2, self.end.hour, self.end.minute)
return (until - dtstart).total_seconds()
Expand Down