Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REGR: Fix reading old pickles from pandas 1.3.5 #56308

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Fixed regression when trying to read a pickled pandas :class:`DataFrame` from pandas 1.3 (:issue:`55137`)
-

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,11 @@ def new_block(
# - check_ndim/ensure_block_shape already checked
# - maybe_coerce_values already called/unnecessary
klass = get_block_type(values.dtype)

# Old pandas (<=1.3.0) will call this function with placements
# that aren't necessarily BlockPlacements when unpickling
if not isinstance(placement, BlockPlacement):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be done in _setstate__ or something? a lot of effort has gone into optimizing this function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also maybe deprecate reading of such-old pickles?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea on deprecating reading old pickles, I'll do that as a follow-up (1.3.5 is 2 years old at this point I think).

How would I change __setstate__? I'm not familiar at all with pickle stuff.

I thought __reduce__ calls new_block directly in 1.3.5.

cpdef __reduce__(self):
# We have to do some gymnastics b/c "ndim" is keyword-only
from functools import partial
from pandas.core.internals.blocks import new_block
args = (self.values, self.mgr_locs.indexer)
func = partial(new_block, ndim=self.ndim)
return func, args

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, __reduce__ sounds like the right place for this

Copy link
Member Author

@lithomas1 lithomas1 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the old __reduce__ (from 1.3.5) is getting called though, which is leading to the problem, since it directly calls new_block.

The newer reduce goes through _unpickle_block, which does the conversion to BlockPlacement for us.

Are we OK just taking the perf hit for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need patching in compat.pickle_compat then. That is always a PITA.

i think id be against taking the perf hit for something so old since it would be hit a zillion times.

placement = BlockPlacement(placement)
return klass(values, ndim=ndim, placement=placement, refs=refs)


Expand Down
Binary file not shown.
9 changes: 8 additions & 1 deletion pandas/tests/io/generate_legacy_storage_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
import platform as pl
import sys

# Remove script directory from path, otherwise Python will try to
# import the JSON test directory as the json module
sys.path.pop(0)

import numpy as np

import pandas
Expand Down Expand Up @@ -314,7 +318,7 @@ def write_legacy_pickles(output_dir):

def write_legacy_file():
# force our cwd to be the first searched
sys.path.insert(0, ".")
sys.path.insert(0, "")

if not 3 <= len(sys.argv) <= 4:
sys.exit(
Expand All @@ -325,6 +329,9 @@ def write_legacy_file():
output_dir = str(sys.argv[1])
storage_type = str(sys.argv[2])

if not os.path.exists(output_dir):
os.mkdir(output_dir)

if storage_type == "pickle":
write_legacy_pickles(output_dir=output_dir)
else:
Expand Down
Loading