Skip to content

BUG: pickle after _with_freq #33811

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
Apr 26, 2020
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
9 changes: 5 additions & 4 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,15 @@ def ceil(self, freq, ambiguous="raise", nonexistent="raise"):

def _with_freq(self, freq):
"""
Helper to set our freq in-place, returning self to allow method chaining.
Helper to get a view on the same data, with a new freq.

Parameters
----------
freq : DateOffset, None, or "infer"

Returns
-------
self
Same type as self
"""
# GH#29843
if freq is None:
Expand All @@ -433,8 +433,9 @@ def _with_freq(self, freq):
assert freq == "infer"
freq = frequencies.to_offset(self.inferred_freq)

self._freq = freq
return self
arr = self.view()
arr._freq = freq
return arr


class DatetimeLikeArrayMixin(
Expand Down
18 changes: 3 additions & 15 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from pandas.core.ops import get_op_result_name
from pandas.core.tools.timedeltas import to_timedelta

from pandas.tseries.frequencies import DateOffset, to_offset
from pandas.tseries.frequencies import DateOffset
from pandas.tseries.offsets import Tick

_index_doc_kwargs = dict(ibase._index_doc_kwargs)
Expand Down Expand Up @@ -678,20 +678,8 @@ def freqstr(self):
return self.freq.freqstr

def _with_freq(self, freq):
index = self.copy(deep=False)
if freq is None:
# Even if we _can_ have a freq, we might want to set it to None
index._freq = None
elif len(self) == 0 and isinstance(freq, DateOffset):
# Always valid. In the TimedeltaArray case, we assume this
# is a Tick offset.
index._freq = freq
else:
assert freq == "infer", freq
freq = to_offset(self.inferred_freq)
index._freq = freq

return index
arr = self._data._with_freq(freq)
return type(self)._simple_new(arr, name=self.name)

def _shallow_copy(self, values=None, name: Label = lib.no_default):
name = self.name if name is lib.no_default else name
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def test_pickle(self):
idx_p = tm.round_trip_pickle(idx)
tm.assert_index_equal(idx, idx_p)

def test_pickle_after_set_freq(self):
dti = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
dti = dti._with_freq(None)

res = tm.round_trip_pickle(dti)
tm.assert_index_equal(res, dti)

def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
# GH7774
index = date_range("20130101", periods=3, tz="US/Eastern")
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def test_shift(self):
def test_pickle_compat_construction(self):
pass

def test_pickle_after_set_freq(self):
tdi = timedelta_range("1 day", periods=4, freq="s")
tdi = tdi._with_freq(None)

res = tm.round_trip_pickle(tdi)
tm.assert_index_equal(res, tdi)

def test_isin(self):

index = tm.makeTimedeltaIndex(4)
Expand Down