Closed
Description
cat = pd.Categorical([3, 1], categories=[3, 2, 1], ordered=True)
res = pd.Categorical(cat, dtype="category")
>>> res.dtype
CategoricalDtype(categories=[3, 2, 1], ordered=False)
>>> cat.dtype
CategoricalDtype(categories=[3, 2, 1], ordered=True)
By contrast, if we do cat.astype("category")
we do retain orderedness.
Found when refactoring sanitize_array
. We have two code paths effectively:
if isinstance(data, ExtensionArray):
if dtype is not None:
return data.astype(dtype, copy=copy)
elif copy:
return data.copy()
return data
if isinstance(dtype, ExtensionDtype):
cls = dtype.construct_array_type()
return cls._from_sequence(data, dtype=dtype, copy=copy)
Switching the order of these checks causes a few Categorical tests to break bc of the Categorical constructor behavior.