Skip to content

Commit

Permalink
Backport PR pandas-dev#37657 on branch 1.1.x: BUG: unpickling modifie…
Browse files Browse the repository at this point in the history
…s Block.ndim
  • Loading branch information
jbrockmendel authored and simonjayhawkins committed Nov 9, 2020
1 parent d601416 commit 856d714
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- Bug in metadata propagation for ``groupby`` iterator (:issue:`37343`)
- Bug in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
-

.. ---------------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,17 @@ def __getstate__(self):
return axes_array, block_values, block_items, extra_state

def __setstate__(self, state):
def unpickle_block(values, mgr_locs):
return make_block(values, placement=mgr_locs)
def unpickle_block(values, mgr_locs, ndim: int):
# TODO(EA2D): ndim would be unnecessary with 2D EAs
return make_block(values, placement=mgr_locs, ndim=ndim)

if isinstance(state, tuple) and len(state) >= 4 and "0.14.1" in state[3]:
state = state[3]["0.14.1"]
self.axes = [ensure_index(ax) for ax in state["axes"]]
ndim = len(self.axes)
self.blocks = tuple(
unpickle_block(b["values"], b["mgr_locs"]) for b in state["blocks"]
unpickle_block(b["values"], b["mgr_locs"], ndim=ndim)
for b in state["blocks"]
)
else:
raise NotImplementedError("pre-0.14.1 pickles are no longer supported")
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,15 @@ def test_read_pickle_with_subclass():

tm.assert_series_equal(result[0], expected[0])
assert isinstance(result[1], MyTz)


def test_pickle_preserves_block_ndim():
# GH#37631
ser = pd.Series(list("abc")).astype("category").iloc[[0]]
res = tm.round_trip_pickle(ser)

assert res._mgr.blocks[0].ndim == 1
assert res._mgr.blocks[0].shape == (1,)

# GH#37631 OP issue was about indexing, underlying problem was pickle
tm.assert_series_equal(res[[True]], ser)

0 comments on commit 856d714

Please sign in to comment.