Skip to content
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

BUG: Fixes to msgpack support. #19975

Merged
merged 3 commits into from
Mar 7, 2018
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
24 changes: 22 additions & 2 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
from pandas import (Timestamp, Period, Series, DataFrame, # noqa
Index, MultiIndex, Float64Index, Int64Index,
Panel, RangeIndex, PeriodIndex, DatetimeIndex, NaT,
Categorical, CategoricalIndex)
Categorical, CategoricalIndex, IntervalIndex, Interval,
TimedeltaIndex)
from pandas.core.sparse.api import SparseSeries, SparseDataFrame
from pandas.core.sparse.array import BlockIndex, IntIndex
from pandas.core.generic import NDFrame
Expand Down Expand Up @@ -401,6 +402,13 @@ def encode(obj):
u'freq': u_safe(getattr(obj, 'freqstr', None)),
u'tz': tz,
u'compress': compressor}
elif isinstance(obj, IntervalIndex):
return {u'typ': u'interval_index',
u'klass': u(obj.__class__.__name__),
u'name': getattr(obj, 'name', None),
u'left': getattr(obj, '_left', None),
u'right': getattr(obj, '_right', None),
u'closed': getattr(obj, '_closed', None)}
elif isinstance(obj, MultiIndex):
return {u'typ': u'multi_index',
u'klass': u(obj.__class__.__name__),
Expand Down Expand Up @@ -513,7 +521,12 @@ def encode(obj):
elif isinstance(obj, Period):
return {u'typ': u'period',
u'ordinal': obj.ordinal,
u'freq': u(obj.freq)}
u'freq': u_safe(obj.freqstr)}
elif isinstance(obj, Interval):
return {u'typ': u'interval',
u'left': obj.left,
u'right': obj.right,
u'closed': obj.closed}
elif isinstance(obj, BlockIndex):
return {u'typ': u'block_index',
u'klass': u(obj.__class__.__name__),
Expand Down Expand Up @@ -597,12 +610,19 @@ def decode(obj):
result = result.tz_localize('UTC').tz_convert(tz)
return result

elif typ == u'interval_index':
return globals()[obj[u'klass']].from_arrays(obj[u'left'],
obj[u'right'],
obj[u'closed'],
name=obj[u'name'])
elif typ == u'category':
from_codes = globals()[obj[u'klass']].from_codes
return from_codes(codes=obj[u'codes'],
categories=obj[u'categories'],
ordered=obj[u'ordered'])

elif typ == u'interval':
return Interval(obj[u'left'], obj[u'right'], obj[u'closed'])
elif typ == u'series':
dtype = dtype_for(obj[u'dtype'])
pd_dtype = pandas_dtype(dtype)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/generate_legacy_storage_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ def create_msgpack_data():
del data['frame']['cat_onecol']
del data['frame']['cat_and_float']
del data['scalars']['period']
del data['index']['interval']
if _loose_version < LooseVersion('0.23.0'):
del data['index']['interval']
del data['offsets']
return _u(data)

Expand Down
20 changes: 18 additions & 2 deletions pandas/tests/io/test_packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from pandas import compat
from pandas.compat import u, PY3
from pandas import (Series, DataFrame, Panel, MultiIndex, bdate_range,
date_range, period_range, Index, Categorical)
date_range, period_range, Index, Categorical,
Period, Interval)
from pandas.errors import PerformanceWarning
from pandas.io.packers import to_msgpack, read_msgpack
import pandas.util.testing as tm
Expand Down Expand Up @@ -317,6 +318,19 @@ def test_timedeltas(self):
i_rec = self.encode_decode(i)
assert i == i_rec

def test_periods(self):
# 13463
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you test period index & interval index explicity. DId you say timedeltaindex is fixed as well? if not can you add a test for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntervalIndex and TimedeltaIndex are now tested in TestIndex along with all the other index subclasses (see addition at lines 352-353). Would it be better to leave that way so all the Index subclasses are in one place and treated the same way rather than separating 352-353 into its own test? E.g. it was because I didn't see TimedeltaIndex in the dict in TestIndex that I added it (and, in turn, found out that TimedeltaIndex was broken).

PeriodIndex worked before (and was tested, see line 334) but the Period object itself couldn't be serialized (#13463). That's being explicitly tested here at line 321 (serialization of Period and Interval).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok that's fine then.

for i in [Period('2010-09', 'M'), Period('2014-Q1', 'Q')]:
i_rec = self.encode_decode(i)
assert i == i_rec

def test_intervals(self):
# 19967
for i in [Interval(0, 1), Interval(0, 1, 'left'),
Interval(10, 25., 'right')]:
i_rec = self.encode_decode(i)
assert i == i_rec


class TestIndex(TestPackers):

Expand All @@ -334,7 +348,9 @@ def setup_method(self, method):
'period': Index(period_range('2012-1-1', freq='M', periods=3)),
'date2': Index(date_range('2013-01-1', periods=10)),
'bdate': Index(bdate_range('2013-01-02', periods=10)),
'cat': tm.makeCategoricalIndex(100)
'cat': tm.makeCategoricalIndex(100),
'interval': tm.makeIntervalIndex(100),
'timedelta': tm.makeTimedeltaIndex(100, 'H')
}

self.mi = {
Expand Down