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

BUG: pd.Categorical(categorical, dtype="category") loses orderedness #49309

Closed
jbrockmendel opened this issue Oct 25, 2022 · 1 comment · Fixed by #49875
Closed

BUG: pd.Categorical(categorical, dtype="category") loses orderedness #49309

jbrockmendel opened this issue Oct 25, 2022 · 1 comment · Fixed by #49875
Assignees
Labels
Bug Categorical Categorical Data Type Constructors Series/DataFrame/Index/pd.array Constructors

Comments

@jbrockmendel
Copy link
Member

jbrockmendel commented Oct 25, 2022

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.

@jbrockmendel jbrockmendel added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2022
@rhshadrach rhshadrach added Categorical Categorical Data Type Constructors Series/DataFrame/Index/pd.array Constructors and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2022
@eshanja1n
Copy link

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Constructors Series/DataFrame/Index/pd.array Constructors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants