Skip to content

BUG: CategoricalDtype is not refresh after index categories set #46820

@Yikun

Description

@Yikun

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
pdf = pd.DataFrame(
          {
              "a": pd.Categorical([1, 2, 3, 1, 2, 3]),
          },
          index=pd.Categorical([10, 20, 30, 20, 30, 10], categories=[30, 10, 20], ordered=True),
      )
pidx = pdf.index
pidx.categories = ["z", "y", "x"]
pidx.dtype

Issue Description

categories set failed

>>> pidx.dtype
CategoricalDtype(categories=[30, 10, 20], ordered=True)

Expected Behavior

>>> pidx.dtype
CategoricalDtype(categories=['z', 'y', 'x'], ordered=True)

(also a behavior before 1.4.x)

Installed Versions

Details

1.4.0+

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCategoricalCategorical Data TypeIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions