Skip to content

Commit 018c494

Browse files
committed
BUG: Fixes to msgpack support.
Adds support for IntervalIndex and Interval; fixes Period and TimedeltaIndex. Closes pandas-dev#19939 and pandas-dev#13463.
1 parent a7a7f8c commit 018c494

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

pandas/io/packers.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
from pandas import (Timestamp, Period, Series, DataFrame, # noqa
5656
Index, MultiIndex, Float64Index, Int64Index,
5757
Panel, RangeIndex, PeriodIndex, DatetimeIndex, NaT,
58-
Categorical, CategoricalIndex)
58+
Categorical, CategoricalIndex, IntervalIndex, Interval,
59+
TimedeltaIndex)
5960
from pandas.core.sparse.api import SparseSeries, SparseDataFrame
6061
from pandas.core.sparse.array import BlockIndex, IntIndex
6162
from pandas.core.generic import NDFrame
@@ -401,6 +402,13 @@ def encode(obj):
401402
u'freq': u_safe(getattr(obj, 'freqstr', None)),
402403
u'tz': tz,
403404
u'compress': compressor}
405+
elif isinstance(obj, IntervalIndex):
406+
return {u'typ': u'interval_index',
407+
u'klass': u(obj.__class__.__name__),
408+
u'name': getattr(obj, 'name', None),
409+
u'left': getattr(obj, '_left', None),
410+
u'right': getattr(obj, '_right', None),
411+
u'closed': getattr(obj, '_closed', None)}
404412
elif isinstance(obj, MultiIndex):
405413
return {u'typ': u'multi_index',
406414
u'klass': u(obj.__class__.__name__),
@@ -513,7 +521,12 @@ def encode(obj):
513521
elif isinstance(obj, Period):
514522
return {u'typ': u'period',
515523
u'ordinal': obj.ordinal,
516-
u'freq': u(obj.freq)}
524+
u'freq': u_safe(obj.freqstr)}
525+
elif isinstance(obj, Interval):
526+
return {u'typ': u'interval',
527+
u'left':obj.left,
528+
u'right':obj.right,
529+
u'closed':obj.closed}
517530
elif isinstance(obj, BlockIndex):
518531
return {u'typ': u'block_index',
519532
u'klass': u(obj.__class__.__name__),
@@ -597,12 +610,19 @@ def decode(obj):
597610
result = result.tz_localize('UTC').tz_convert(tz)
598611
return result
599612

613+
elif typ == u'interval_index':
614+
return globals()[obj[u'klass']].from_arrays(obj[u'left'],
615+
obj[u'right'],
616+
obj[u'closed'],
617+
name=obj[u'name'])
600618
elif typ == u'category':
601619
from_codes = globals()[obj[u'klass']].from_codes
602620
return from_codes(codes=obj[u'codes'],
603621
categories=obj[u'categories'],
604622
ordered=obj[u'ordered'])
605623

624+
elif typ == u'interval':
625+
return Interval(obj[u'left'], obj[u'right'], obj[u'closed'])
606626
elif typ == u'series':
607627
dtype = dtype_for(obj[u'dtype'])
608628
pd_dtype = pandas_dtype(dtype)

pandas/tests/io/test_packers.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from pandas import compat
1111
from pandas.compat import u, PY3
1212
from pandas import (Series, DataFrame, Panel, MultiIndex, bdate_range,
13-
date_range, period_range, Index, Categorical)
13+
date_range, period_range, Index, Categorical,
14+
Period, Interval)
1415
from pandas.errors import PerformanceWarning
1516
from pandas.io.packers import to_msgpack, read_msgpack
1617
import pandas.util.testing as tm
@@ -317,6 +318,19 @@ def test_timedeltas(self):
317318
i_rec = self.encode_decode(i)
318319
assert i == i_rec
319320

321+
def test_periods(self):
322+
# 13463
323+
for i in [Period('2010-09', 'M'), Period('2014-Q1', 'Q')]:
324+
i_rec = self.encode_decode(i)
325+
assert i == i_rec
326+
327+
def test_intervals(self):
328+
# 19967
329+
for i in [Interval(0, 1), Interval(0, 1, 'left'),
330+
Interval(10, 25., 'right')]:
331+
i_rec = self.encode_decode(i)
332+
assert i == i_rec
333+
320334

321335
class TestIndex(TestPackers):
322336

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

340356
self.mi = {

0 commit comments

Comments
 (0)