Skip to content

ENH: CategoricalDtype __repr__ should show dtype of categories #52179

Closed
@phofl

Description

@phofl

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Right now the repr only shows the categories and if they are ordered, but since we can back Categoricals with arrow or nullable dtypes or generally different NumPy dtypes (e.g. int32) we should also show the dtype of the categories:

ser1 = Series(pd.Categorical(Index([1, 2, 3], dtype="int32")))
ser2 = Series(pd.Categorical(Index([1, 2, 3], dtype="int64")))

tm.assert_series_equal(ser1, ser2)

This shows

Attribute "dtype" are different
[left]:  CategoricalDtype(categories=[1, 2, 3], ordered=False)
[right]: CategoricalDtype(categories=[1, 2, 3], ordered=False)

which is not very helpful.

Feature Description

Add something like category_dtype="int32" to the repr.

Alternative Solutions

Not sure what you could do apart from checking explicitly for the categories dtype yourself.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions