8000 ENH: Period pickle by jreback · Pull Request #10866 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Period pickle #10866

8000 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 2 commits into from
Aug 20, 2015
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ Other enhancements

``tolerance`` is also exposed by the lower level ``Index.get_indexer`` and ``Index.get_loc`` methods.

- Support pickling of ``Period`` objects (:issue:`10439`)

.. _whatsnew_0170.api:

.. _whatsnew_0170.api_breaking:
Expand Down
Binary file not shown.
Binary file not shown.
10 changes: 9 additions & 1 deletion pandas/io/tests/generate_legacy_storage_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from pandas import (Series, TimeSeries, DataFrame, Panel,
SparseSeries, SparseTimeSeries, SparseDataFrame, SparsePanel,
Index, MultiIndex, PeriodIndex, bdate_range, to_msgpack,
date_range, period_range, bdate_range, Timestamp, Categorical)
date_range, period_range, bdate_range, Timestamp, Categorical,
Period)
import os
import sys
import numpy as np
Expand Down Expand Up @@ -63,6 +64,10 @@ def create_data():
'E': [0., 1, Timestamp('20100101'), 'foo', 2.]
}

scalars = dict(timestamp=Timestamp('20130101'))
if LooseVersion(pandas.__version__) >= '0.17.0':
scalars['period'] = Period('2012','M')

index = dict(int=Index(np.arange(10)),
date=date_range('20130101', periods=10),
period=period_range('2013-01-01', freq='M', periods=10))
Expand All @@ -79,6 +84,8 @@ def create_data():
names=['one', 'two'])),
dup=Series(np.arange(5).astype(np.float64), index=['A', 'B', 'C', 'D', 'A']),
cat=Series(Categorical(['foo', 'bar', 'baz'])))
if LooseVersion(pandas.__version__) >= '0.17.0':
series['period'] = Series([Period('2000Q1')] * 5)

mixed_dup_df = DataFrame(data)
mixed_dup_df.columns = list("ABCDA")
Expand Down Expand Up @@ -107,6 +114,7 @@ def create_data():
frame=frame,
panel=panel,
index=index,
scalars=scalars,
mi=mi,
sp_series=dict(float=_create_sp_series(),
ts=_create_sp_tsseries()),
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def compare_element(self, typ, result, expected):
comparator = getattr(test_sparse,"assert_%s_equal" % typ)
comparator(result,expected,exact_indices=False)
else:
comparator = getattr(tm,"assert_%s_equal" % typ)
comparator = getattr(tm,"assert_%s_equal" % typ,tm.assert_almost_equal)
comparator(result,expected)

def compare(self, vf):
Expand Down
8 changes: 8 additions & 0 deletions pandas/src/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,14 @@ cdef class Period(object):
value = ("%s" % formatted)
return value

def __setstate__(self, state):
self.freq=state[1]
self.ordinal=state[2]

def __reduce__(self):
object_state = None, self.freq, self.ordinal
return (Period, object_state)

def strftime(self, fmt):
"""
Returns the string representation of the :class:`Period`, depending
Expand Down
7 changes: 6 additions & 1 deletion pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,6 @@ def test_append_concat(self):

def test_pickle_freq(self):
# GH2891
import pickle
prng = period_range('1/1/2011', '1/1/2012', freq='M')
new_prng = self.round_trip_pickle(prng)
self.assertEqual(new_prng.freq,'M')
Expand Down Expand Up @@ -2536,6 +2535,12 @@ def test_searchsorted(self):
ValueError, 'Different period frequency: H',
lambda: pidx.searchsorted(pd.Period('2014-01-01', freq='H')))

def test_round_trip(self):

p = Period('2000Q1')
new_p = self.round_trip_pickle(p)
self.assertEqual(new_p, p)

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

Expand Down
0