Skip to content

BUG: Preserve CategoricalDtype._ordered_from_sentinel with pickle #27317

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

Merged
Merged
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
3 changes: 2 additions & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):
kind = "O" # type: str_type
str = "|O08"
base = np.dtype("O")
_metadata = ("categories", "ordered")
_metadata = ("categories", "ordered", "_ordered_from_sentinel")
_cache = {} # type: Dict[str_type, PandasExtensionDtype]

def __init__(self, categories=None, ordered: OrderedType = ordered_sentinel):
Expand Down Expand Up @@ -356,6 +356,7 @@ def __setstate__(self, state: Dict[str_type, Any]) -> None:
# pickle -> need to set the settable private ones here (see GH26067)
self._categories = state.pop("categories", None)
self._ordered = state.pop("ordered", False)
self._ordered_from_sentinel = state.pop("_ordered_from_sentinel", False)

def __hash__(self) -> int:
# _hash_categories returns a uint64, so use the negative
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,19 @@ def test_ordered_none_default_deprecated(self, ordered):
with tm.assert_produces_warning(warning):
dtype.ordered

@pytest.mark.parametrize("ordered", [True, False, None, ordered_sentinel])
def test_pickle_ordered_from_sentinel(self, ordered):
# GH 27295: can remove test when _ordered_from_sentinel is removed (GH 26403)
dtype = CategoricalDtype(categories=list("abc"), ordered=ordered)

warning = FutureWarning if ordered is ordered_sentinel else None
with tm.assert_produces_warning(warning, check_stacklevel=False):
dtype_from_pickle = tm.round_trip_pickle(dtype)

result = dtype_from_pickle._ordered_from_sentinel
expected = ordered is ordered_sentinel
assert result is expected


@pytest.mark.parametrize(
"dtype", [CategoricalDtype, IntervalDtype, DatetimeTZDtype, PeriodDtype]
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ def test_pickle_preserve_name(self):
unpickled = self._pickle_roundtrip_name(tm.makeTimeSeries(name=n))
assert unpickled.name == n

def test_pickle_categorical_ordered_from_sentinel(self):
# GH 27295: can remove test when _ordered_from_sentinel is removed (GH 26403)
s = Series(["a", "b", "c", "a"], dtype="category")
result = tm.round_trip_pickle(s)
result = result.astype("category")

tm.assert_series_equal(result, s)
assert result.dtype._ordered_from_sentinel is False

def _pickle_roundtrip_name(self, obj):

with ensure_clean() as path:
Expand Down