Skip to content

Commit dbee8fa

Browse files
authored
BUG: unpickling modifies Block.ndim (#37657)
1 parent 2d8871f commit dbee8fa

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
@@ -278,14 +278,17 @@ def __getstate__(self):
278278
return axes_array, block_values, block_items, extra_state
279279

280280
def __setstate__(self, state):
281-
def unpickle_block(values, mgr_locs):
282-
return make_block(values, placement=mgr_locs)
281+
def unpickle_block(values, mgr_locs, ndim: int):
282+
# TODO(EA2D): ndim would be unnecessary with 2D EAs
283+
return make_block(values, placement=mgr_locs, ndim=ndim)
283284

284285
if isinstance(state, tuple) and len(state) >= 4 and "0.14.1" in state[3]:
285286
state = state[3]["0.14.1"]
286287
self.axes = [ensure_index(ax) for ax in state["axes"]]
288+
ndim = len(self.axes)
287289
self.blocks = tuple(
288-
unpickle_block(b["values"], b["mgr_locs"]) for b in state["blocks"]
290+
unpickle_block(b["values"], b["mgr_locs"], ndim=ndim)
291+
for b in state["blocks"]
289292
)
290293
else:
291294
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
@@ -576,3 +576,15 @@ def test_pickle_datetimes(datetime_series):
576576
def test_pickle_strings(string_series):
577577
unp_series = tm.round_trip_pickle(string_series)
578578
tm.assert_series_equal(unp_series, string_series)
579+
580+
581+
def test_pickle_preserves_block_ndim():
582+
# GH#37631
583+
ser = Series(list("abc")).astype("category").iloc[[0]]
584+
res = tm.round_trip_pickle(ser)
585+
586+
assert res._mgr.blocks[0].ndim == 1
587+
assert res._mgr.blocks[0].shape == (1,)
588+
589+
# GH#37631 OP issue was about indexing, underlying problem was pickle
590+
tm.assert_series_equal(res[[True]], ser)

0 commit comments

Comments
 (0)