Skip to content

Commit 856d714

Browse files
jbrockmendelsimonjayhawkins
authored andcommitted
Backport PR pandas-dev#37657 on branch 1.1.x: BUG: unpickling modifies Block.ndim
1 parent d601416 commit 856d714

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

doc/source/whatsnew/v1.1.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Fixed regressions
2424
Bug fixes
2525
~~~~~~~~~
2626
- Bug in metadata propagation for ``groupby`` iterator (:issue:`37343`)
27+
- Bug in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
2728
-
2829

2930
.. ---------------------------------------------------------------------------

pandas/core/internals/managers.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,17 @@ def __getstate__(self):
284284
return axes_array, block_values, block_items, extra_state
285285

286286
def __setstate__(self, state):
287-
def unpickle_block(values, mgr_locs):
288-
return make_block(values, placement=mgr_locs)
287+
def unpickle_block(values, mgr_locs, ndim: int):
288+
# TODO(EA2D): ndim would be unnecessary with 2D EAs
289+
return make_block(values, placement=mgr_locs, ndim=ndim)
289290

290291
if isinstance(state, tuple) and len(state) >= 4 and "0.14.1" in state[3]:
291292
state = state[3]["0.14.1"]
292293
self.axes = [ensure_index(ax) for ax in state["axes"]]
294+
ndim = len(self.axes)
293295
self.blocks = tuple(
294-
unpickle_block(b["values"], b["mgr_locs"]) for b in state["blocks"]
296+
unpickle_block(b["values"], b["mgr_locs"], ndim=ndim)
297+
for b in state["blocks"]
295298
)
296299
else:
297300
raise NotImplementedError("pre-0.14.1 pickles are no longer supported")

pandas/tests/io/test_pickle.py

+12
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,15 @@ def test_read_pickle_with_subclass():
477477

478478
tm.assert_series_equal(result[0], expected[0])
479479
assert isinstance(result[1], MyTz)
480+
481+
482+
def test_pickle_preserves_block_ndim():
483+
# GH#37631
484+
ser = pd.Series(list("abc")).astype("category").iloc[[0]]
485+
res = tm.round_trip_pickle(ser)
486+
487+
assert res._mgr.blocks[0].ndim == 1
488+
assert res._mgr.blocks[0].shape == (1,)
489+
490+
# GH#37631 OP issue was about indexing, underlying problem was pickle
491+
tm.assert_series_equal(res[[True]], ser)

0 commit comments

Comments
 (0)