Skip to content

[BUG] fixed DateOffset pickle bug when months >= 12 #35258

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 9 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ Datetimelike
resolution which converted to object dtype instead of coercing to ``datetime64[ns]``
dtype when within the timestamp bounds (:issue:`34843`).
- The ``freq`` keyword in :class:`Period`, :func:`date_range`, :func:`period_range`, :func:`pd.tseries.frequencies.to_offset` no longer allows tuples, pass as string instead (:issue:`34703`)
- Bug in :class:`DateOffset` where attributes reconstructed from pickle files differ from original objects when input values exceed normal ranges (e.g months=12) (:issue:`34511`)

Timedelta
^^^^^^^^^
Expand Down
7 changes: 0 additions & 7 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -938,13 +938,6 @@ cdef class RelativeDeltaOffset(BaseOffset):
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.n = state.pop("n")
self.normalize = state.pop("normalize")
self._cache = state.pop("_cache", {})
Expand Down
183 changes: 0 additions & 183 deletions pandas/tests/tseries/offsets/data/dateoffset_0_15_2.pickle

This file was deleted.

Binary file not shown.
15 changes: 12 additions & 3 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def test_add_empty_datetimeindex(self, offset_types, tz_naive_fixture):
result = offset_s + dta
tm.assert_equal(result, dta)

def test_pickle_v0_15_2(self, datapath):
def test_pickle_v1_1_0(self, datapath):
offsets = {
"DateOffset": DateOffset(years=1),
"MonthBegin": MonthBegin(1),
Expand All @@ -644,8 +644,8 @@ def test_pickle_v0_15_2(self, datapath):
"Week": Week(1),
}

pickle_path = datapath("tseries", "offsets", "data", "dateoffset_0_15_2.pickle")
# This code was executed once on v0.15.2 to generate the pickle:
pickle_path = datapath("tseries", "offsets", "data", "dateoffset_1_1_0.pickle")
# This code was executed once on v1.1.0 to generate the pickle:
# with open(pickle_path, 'wb') as f: pickle.dump(offsets, f)
#
result = read_pickle(pickle_path)
Expand All @@ -664,6 +664,15 @@ def test_pickle_roundtrip(self, offset_types):
# Make sure nothings got lost from _params (which __eq__) is based on
assert getattr(off, attr) == getattr(res, attr)

def test_pickle_dateoffset_odd_inputs(self):
# GH#34511
off = DateOffset(months=12)
res = tm.round_trip_pickle(off)
assert off == res

base_dt = datetime(2020, 1, 1)
assert base_dt + off == base_dt + res

def test_onOffset_deprecated(self, offset_types):
# GH#30340 use idiomatic naming
off = self._get_offset(offset_types)
Expand Down