Skip to content

Commit

Permalink
BUG: Fixes to msgpack support. (pandas-dev#19975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam3851 authored and jreback committed Mar 7, 2018
1 parent fd010de commit 2532a49
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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
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

0 comments on commit 2532a49

Please sign in to comment.