Skip to content

Commit 9bb2bee

Browse files
committed
BUG: Properly read Categorical msgpacks
Patches bug in read_msgpack in which Series categoricals were accidentally being constructed with a non-categorical dtype, resulting in an error. Closes gh-14901.
1 parent f1cfe5b commit 9bb2bee

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ Bug Fixes
245245

246246

247247

248+
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)
248249

249250

250251

pandas/io/packers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,14 @@ def decode(obj):
595595
pd_dtype = pandas_dtype(dtype)
596596
np_dtype = pandas_dtype(dtype).base
597597

598+
# see gh-14901
599+
klass_dtype = np_dtype if pd_dtype.name != 'category' else pd_dtype
600+
598601
index = obj[u'index']
599602
result = globals()[obj[u'klass']](unconvert(obj[u'data'], dtype,
600603
obj[u'compress']),
601604
index=index,
602-
dtype=np_dtype,
605+
dtype=klass_dtype,
603606
name=obj[u'name'])
604607
tz = getattr(pd_dtype, 'tz', None)
605608
if tz:

pandas/io/tests/test_packers.py

+4
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,17 @@ def setUp(self):
363363
'F': [Timestamp('20130102', tz='US/Eastern')] * 2 +
364364
[Timestamp('20130603', tz='CET')] * 3,
365365
'G': [Timestamp('20130102', tz='US/Eastern')] * 5,
366+
'H': Categorical([1, 2, 3, 4, 5]),
367+
'I': Categorical([1, 2, 3, 4, 5], ordered=True),
366368
}
367369

368370
self.d['float'] = Series(data['A'])
369371
self.d['int'] = Series(data['B'])
370372
self.d['mixed'] = Series(data['E'])
371373
self.d['dt_tz_mixed'] = Series(data['F'])
372374
self.d['dt_tz'] = Series(data['G'])
375+
self.d['cat_ordered'] = Series(data['H'])
376+
self.d['cat_unordered'] = Series(data['I'])
373377

374378
def test_basic(self):
375379

0 commit comments

Comments
 (0)