Skip to content

Commit e8702f8

Browse files
committed
BUG: categorical unpickle to use _coerce_indexer_dtype
1 parent f20b41e commit e8702f8

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

pandas/core/categorical.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -999,11 +999,12 @@ def __setstate__(self, state):
999999
raise Exception('invalid pickle state')
10001000

10011001
# Provide compatibility with pre-0.15.0 Categoricals.
1002-
if '_codes' not in state and 'labels' in state:
1003-
state['_codes'] = state.pop('labels').astype(np.int8)
10041002
if '_categories' not in state and '_levels' in state:
10051003
state['_categories'] = self._validate_categories(state.pop(
10061004
'_levels'))
1005+
if '_codes' not in state and 'labels' in state:
1006+
state['_codes'] = _coerce_indexer_dtype(state.pop('labels'),
1007+
state['_categories'])
10071008

10081009
# 0.16.0 ordered change
10091010
if '_ordered' not in state:
Binary file not shown.
Binary file not shown.
Binary file not shown.

pandas/io/tests/generate_legacy_storage_files.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def create_data():
8080
[u'one', u'two', u'one', u'two', u'one',
8181
u'two', u'one', u'two']])),
8282
names=[u'first', u'second']))
83+
8384
series = dict(float=Series(data[u'A']),
8485
int=Series(data[u'B']),
8586
mixed=Series(data[u'E']),
@@ -135,6 +136,10 @@ def create_data():
135136
items=[u'A', u'B', u'A']),
136137
mixed_dup=mixed_dup_panel)
137138

139+
cat = dict(int8=Categorical(list('abcdefg')),
140+
int16=Categorical(np.arange(1000)),
141+
int32=Categorical(np.arange(10000)))
142+
138143
return dict(series=series,
139144
frame=frame,
140145
panel=panel,
@@ -143,7 +148,8 @@ def create_data():
143148
mi=mi,
144149
sp_series=dict(float=_create_sp_series(),
145150
ts=_create_sp_tsseries()),
146-
sp_frame=dict(float=_create_sp_frame()))
151+
sp_frame=dict(float=_create_sp_frame()),
152+
cat=cat)
147153

148154

149155
def create_pickle_data():

pandas/io/tests/test_pickle.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ def compare_series_dt_tz(self, result, expected, typ, version):
109109
tm.assert_series_equal(result, expected)
110110

111111
def compare_series_cat(self, result, expected, typ, version):
112-
# Categorical.ordered is changed in < 0.16.0
113-
if LooseVersion(version) < '0.16.0':
112+
# Categorical dtype is added in 0.15.0
113+
# ordered is changed in 0.16.0
114+
if LooseVersion(version) < '0.15.0':
115+
tm.assert_series_equal(result, expected, check_dtype=False,
116+
check_categorical=False)
117+
elif LooseVersion(version) < '0.16.0':
114118
tm.assert_series_equal(result, expected, check_categorical=False)
115119
else:
116120
tm.assert_series_equal(result, expected)
@@ -125,8 +129,12 @@ def compare_frame_dt_mixed_tzs(self, result, expected, typ, version):
125129
tm.assert_frame_equal(result, expected)
126130

127131
def compare_frame_cat_onecol(self, result, expected, typ, version):
128-
# Categorical.ordered is changed in < 0.16.0
129-
if LooseVersion(version) < '0.16.0':
132+
# Categorical dtype is added in 0.15.0
133+
# ordered is changed in 0.16.0
134+
if LooseVersion(version) < '0.15.0':
135+
tm.assert_frame_equal(result, expected, check_dtype=False,
136+
check_categorical=False)
137+
elif LooseVersion(version) < '0.16.0':
130138
tm.assert_frame_equal(result, expected, check_categorical=False)
131139
else:
132140
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)