Skip to content

BUG: Fix unpickling of string dtypes of legacy pandas versions #61770

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
from pandas.io.formats import printing

if TYPE_CHECKING:
from collections.abc import MutableMapping

import pyarrow

from pandas._typing import (
Expand Down Expand Up @@ -218,6 +220,10 @@ def __eq__(self, other: object) -> bool:
return self.storage == other.storage and self.na_value is other.na_value
return False

def __setstate__(self, state: MutableMapping[str, Any]) -> None:
self.storage = state.pop("storage", "python")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.storage = state.pop("storage", "python")
# back-compat for pandas < 2.3, where na_value did not yet exist
self.storage = state.pop("storage", "python")

self._na_value = state.pop("_na_value", libmissing.NA)

def __hash__(self) -> int:
# need to override __hash__ as well because of overriding __eq__
return super().__hash__()
Expand Down
Binary file not shown.
8 changes: 8 additions & 0 deletions pandas/tests/io/generate_legacy_storage_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def create_pickle_data():
"float": Index(np.arange(10, dtype=np.float64)),
"uint": Index(np.arange(10, dtype=np.uint64)),
"timedelta": timedelta_range("00:00:00", freq="30min", periods=10),
"string": Index(["foo", "bar", "baz", "qux", "quux"], dtype="string"),
}

index["range"] = RangeIndex(10)
Expand Down Expand Up @@ -185,6 +186,7 @@ def create_pickle_data():
"dt": Series(date_range("20130101", periods=5)),
"dt_tz": Series(date_range("20130101", periods=5, tz="US/Eastern")),
"period": Series([Period("2000Q1")] * 5),
"string": Series(["foo", "bar", "baz", "qux", "quux"], dtype="string"),
}

mixed_dup_df = DataFrame(data)
Expand Down Expand Up @@ -233,6 +235,12 @@ def create_pickle_data():
},
index=range(5),
),
"string": DataFrame(
{
"A": Series(["foo", "bar", "baz", "qux", "quux"], dtype="string"),
"B": Series(["one", "two", "one", "two", "three"], dtype="string"),
}
),
}

cat = {
Expand Down
Loading