Skip to content

PeriodIndex pickle roundtrip does not recreate freq GH2891 #3224

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
2 commits merged into from Mar 30, 2013
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
10 changes: 10 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,16 @@ def test_timeseries_repr_object_dtype(self):
ts2 = ts.ix[np.random.randint(0, len(ts) - 1, 400)]
repr(ts).splitlines()[-1]

def test_timeseries_periodindex(self):
# GH2891
import pickle
from pandas import period_range
prng = period_range('1/1/2011', '1/1/2012', freq='M')
ts = Series(np.random.randn(len(prng)), prng)
new_ts = pickle.loads(pickle.dumps(ts))
self.assertEqual(new_ts.index.freq,'M')


def test_iter(self):
for i, val in enumerate(self.series):
self.assertEqual(val, self.series[i])
Expand Down
19 changes: 19 additions & 0 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,25 @@ def append(self, other):
for x in to_concat]
return Index(com._concat_compat(to_concat), name=name)

def __reduce__(self):
"""Necessary for making this object picklable"""
object_state = list(np.ndarray.__reduce__(self))
subclass_state = (self.name, self.freq)
object_state[2] = (object_state[2], subclass_state)
return tuple(object_state)

def __setstate__(self, state):
"""Necessary for making this object picklable"""
if len(state) == 2:
nd_state, own_state = state
np.ndarray.__setstate__(self, nd_state)
self.name = own_state[0]
try: # backcompat
self.freq = own_state[1]
except:
pass
else: # pragma: no cover
np.ndarray.__setstate__(self, state)

def _get_ordinal_range(start, end, periods, freq):
if com._count_not_none(start, end, periods) < 2:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,12 @@ def test_append_concat(self):
self.assert_(isinstance(result.index, PeriodIndex))
self.assertEquals(result.index[0], s1.index[0])

def test_pickle_freq(self):
# GH2891
import pickle
prng = period_range('1/1/2011', '1/1/2012', freq='M')
new_prng = pickle.loads(pickle.dumps(prng))
self.assertEqual(new_prng.freq,'M')

def _permute(obj):
return obj.take(np.random.permutation(len(obj)))
Expand Down