Skip to content

Commit 3f8c0c4

Browse files
topper-123WillAyd
authored andcommitted
COMPAT: remove Categorical pickle compat with Pandas < 0.16 (#27538)
1 parent 5ca9672 commit 3f8c0c4

File tree

7 files changed

+4
-145
lines changed

7 files changed

+4
-145
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Removal of prior version deprecations/changes
109109
- :meth:`pandas.Series.str.cat` does not accept list-likes *within* list-likes anymore (:issue:`27611`)
110110
- Removed the previously deprecated :meth:`ExtensionArray._formatting_values`. Use :attr:`ExtensionArray._formatter` instead. (:issue:`23601`)
111111
- Removed the previously deprecated ``IntervalIndex.from_intervals`` in favor of the :class:`IntervalIndex` constructor (:issue:`19263`)
112+
- Ability to read pickles containing :class:`Categorical` instances created with pre-0.16 version of pandas has been removed (:issue:`27538`)
113+
-
112114

113115
.. _whatsnew_1000.performance:
114116

pandas/core/arrays/categorical.py

+1-18
Original file line numberDiff line numberDiff line change
@@ -1353,24 +1353,7 @@ def __setstate__(self, state):
13531353
if not isinstance(state, dict):
13541354
raise Exception("invalid pickle state")
13551355

1356-
# Provide compatibility with pre-0.15.0 Categoricals.
1357-
if "_categories" not in state and "_levels" in state:
1358-
state["_categories"] = self.dtype.validate_categories(state.pop("_levels"))
1359-
if "_codes" not in state and "labels" in state:
1360-
state["_codes"] = coerce_indexer_dtype(
1361-
state.pop("labels"), state["_categories"]
1362-
)
1363-
1364-
# 0.16.0 ordered change
1365-
if "_ordered" not in state:
1366-
1367-
# >=15.0 < 0.16.0
1368-
if "ordered" in state:
1369-
state["_ordered"] = state.pop("ordered")
1370-
else:
1371-
state["_ordered"] = False
1372-
1373-
# 0.21.0 CategoricalDtype change
1356+
# compat with pre 0.21.0 CategoricalDtype change
13741357
if "_dtype" not in state:
13751358
state["_dtype"] = CategoricalDtype(state["_categories"], state["_ordered"])
13761359

578 Bytes
Binary file not shown.

pandas/tests/io/data/categorical_0_14_1.pickle

-94
This file was deleted.
-392 Bytes
Binary file not shown.

pandas/tests/io/test_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def test_read_expands_user_home_dir(
222222
(pd.read_sas, "os", ("io", "sas", "data", "test1.sas7bdat")),
223223
(pd.read_json, "os", ("io", "json", "data", "tsframe_v012.json")),
224224
(pd.read_msgpack, "os", ("io", "msgpack", "data", "frame.mp")),
225-
(pd.read_pickle, "os", ("io", "data", "categorical_0_14_1.pickle")),
225+
(pd.read_pickle, "os", ("io", "data", "categorical.0.25.0.pickle")),
226226
],
227227
)
228228
def test_read_fspath_all(self, reader, module, path, datapath):

pandas/tests/io/test_pickle.py

-32
Original file line numberDiff line numberDiff line change
@@ -184,38 +184,6 @@ def python_unpickler(path):
184184
compare_element(result, expected, typ)
185185

186186

187-
def test_pickle_v0_14_1(datapath):
188-
189-
cat = pd.Categorical(
190-
values=["a", "b", "c"], ordered=False, categories=["a", "b", "c", "d"]
191-
)
192-
pickle_path = datapath("io", "data", "categorical_0_14_1.pickle")
193-
# This code was executed once on v0.14.1 to generate the pickle:
194-
#
195-
# cat = Categorical(labels=np.arange(3), levels=['a', 'b', 'c', 'd'],
196-
# name='foobar')
197-
# with open(pickle_path, 'wb') as f: pickle.dump(cat, f)
198-
#
199-
tm.assert_categorical_equal(cat, pd.read_pickle(pickle_path))
200-
201-
202-
def test_pickle_v0_15_2(datapath):
203-
# ordered -> _ordered
204-
# GH 9347
205-
206-
cat = pd.Categorical(
207-
values=["a", "b", "c"], ordered=False, categories=["a", "b", "c", "d"]
208-
)
209-
pickle_path = datapath("io", "data", "categorical_0_15_2.pickle")
210-
# This code was executed once on v0.15.2 to generate the pickle:
211-
#
212-
# cat = Categorical(labels=np.arange(3), levels=['a', 'b', 'c', 'd'],
213-
# name='foobar')
214-
# with open(pickle_path, 'wb') as f: pickle.dump(cat, f)
215-
#
216-
tm.assert_categorical_equal(cat, pd.read_pickle(pickle_path))
217-
218-
219187
def test_pickle_path_pathlib():
220188
df = tm.makeDataFrame()
221189
result = tm.round_trip_pathlib(df.to_pickle, pd.read_pickle)

0 commit comments

Comments
 (0)