Skip to content

BUG: Bug fix implement __reduce__/__setstate__ for Period pickle support #10441

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

Closed
wants to merge 17 commits into from
Closed
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
3 changes: 2 additions & 1 deletion pandas/io/tests/generate_legacy_storage_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def create_data():
index=MultiIndex.from_tuples(tuple(zip(*[[1, 1, 2, 2, 2], [3, 4, 3, 4, 5]])),
names=['one', 'two'])),
dup=Series(np.arange(5).astype(np.float64), index=['A', 'B', 'C', 'D', 'A']),
cat=Series(Categorical(['foo', 'bar', 'baz'])))
cat=Series(Categorical(['foo', 'bar', 'baz'])),
per=Series([Period('2000Q1')] * 5))

mixed_dup_df = DataFrame(data)
mixed_dup_df.columns = list("ABCDA")
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
11 changes: 11 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

"""

import pickle
import os

from datetime import datetime, date, timedelta

from numpy.ma.testutils import assert_equal
Expand Down Expand Up @@ -2536,6 +2539,14 @@ def test_searchsorted(self):
ValueError, 'Different period frequency: H',
lambda: pidx.searchsorted(pd.Period('2014-01-01', freq='H')))

def test_round_trip(self):

import pickle
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