Skip to content

Commit f420e4d

Browse files
committed
Merge pull request #10866 from jreback/period_pickle
ENH: Period pickle
2 parents 873bbc2 + aa04812 commit f420e4d

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

doc/source/whatsnew/v0.17.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ Other enhancements
198198

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

201+
- Support pickling of ``Period`` objects (:issue:`10439`)
202+
201203
.. _whatsnew_0170.api:
202204

203205
.. _whatsnew_0170.api_breaking:
Binary file not shown.
Binary file not shown.

pandas/io/tests/generate_legacy_storage_files.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from pandas import (Series, TimeSeries, DataFrame, Panel,
55
SparseSeries, SparseTimeSeries, SparseDataFrame, SparsePanel,
66
Index, MultiIndex, PeriodIndex, bdate_range, to_msgpack,
7-
date_range, period_range, bdate_range, Timestamp, Categorical)
7+
date_range, period_range, bdate_range, Timestamp, Categorical,
8+
Period)
89
import os
910
import sys
1011
import numpy as np
@@ -63,6 +64,10 @@ def create_data():
6364
'E': [0., 1, Timestamp('20100101'), 'foo', 2.]
6465
}
6566

67+
scalars = dict(timestamp=Timestamp('20130101'))
68+
if LooseVersion(pandas.__version__) >= '0.17.0':
69+
scalars['period'] = Period('2012','M')
70+
6671
index = dict(int=Index(np.arange(10)),
6772
date=date_range('20130101', periods=10),
6873
period=period_range('2013-01-01', freq='M', periods=10))
@@ -79,6 +84,8 @@ def create_data():
7984
names=['one', 'two'])),
8085
dup=Series(np.arange(5).astype(np.float64), index=['A', 'B', 'C', 'D', 'A']),
8186
cat=Series(Categorical(['foo', 'bar', 'baz'])))
87+
if LooseVersion(pandas.__version__) >= '0.17.0':
88+
series['period'] = Series([Period('2000Q1')] * 5)
8289

8390
mixed_dup_df = DataFrame(data)
8491
mixed_dup_df.columns = list("ABCDA")
@@ -107,6 +114,7 @@ def create_data():
107114
frame=frame,
108115
panel=panel,
109116
index=index,
117+
scalars=scalars,
110118
mi=mi,
111119
sp_series=dict(float=_create_sp_series(),
112120
ts=_create_sp_tsseries()),

pandas/io/tests/test_pickle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def compare_element(self, typ, result, expected):
4848
comparator = getattr(test_sparse,"assert_%s_equal" % typ)
4949
comparator(result,expected,exact_indices=False)
5050
else:
51-
comparator = getattr(tm,"assert_%s_equal" % typ)
51+
comparator = getattr(tm,"assert_%s_equal" % typ,tm.assert_almost_equal)
5252
comparator(result,expected)
5353

5454
def compare(self, vf):

pandas/src/period.pyx

+8
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,14 @@ cdef class Period(object):
969969
value = ("%s" % formatted)
970970
return value
971971

972+
def __setstate__(self, state):
973+
self.freq=state[1]
974+
self.ordinal=state[2]
975+
976+
def __reduce__(self):
977+
object_state = None, self.freq, self.ordinal
978+
return (Period, object_state)
979+
972980
def strftime(self, fmt):
973981
"""
974982
Returns the string representation of the :class:`Period`, depending

pandas/tseries/tests/test_period.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,6 @@ def test_append_concat(self):
24712471

24722472
def test_pickle_freq(self):
24732473
# GH2891
2474-
import pickle
24752474
prng = period_range('1/1/2011', '1/1/2012', freq='M')
24762475
new_prng = self.round_trip_pickle(prng)
24772476
self.assertEqual(new_prng.freq,'M')
@@ -2536,6 +2535,12 @@ def test_searchsorted(self):
25362535
ValueError, 'Different period frequency: H',
25372536
lambda: pidx.searchsorted(pd.Period('2014-01-01', freq='H')))
25382537

2538+
def test_round_trip(self):
2539+
2540+
p = Period('2000Q1')
2541+
new_p = self.round_trip_pickle(p)
2542+
self.assertEqual(new_p, p)
2543+
25392544
def _permute(obj):
25402545
return obj.take(np.random.permutation(len(obj)))
25412546

0 commit comments

Comments
 (0)