Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Infer categorical order from ordered pandas categoricals #158

Description

@joelostblom

It would be nice if px could infer the order of categorical columns if they are already set in pandas.

species_cat = pd.CategoricalDtype(categories=['virginica', 'versicolor', 'setosa'], ordered=True)
iris['species2'] = iris['species'].astype(species_cat)
px.histogram(iris, 'species2')

image

One approach to solve this might be to check the column data types for all columns mapped to a visual element and if they are ordered pandas categoricals, set the plot order via category_orders to be df['col'].cat.categories. So, essentially automating the below:

px.histogram(iris, 'species2', category_orders={'species2': iris['species2'].cat.categories})

image

I am not sure if there would be any cases when it would be disadvantageous to use the pandas order. At least for x, y, color, and facets I think it makes sense, thoughts?

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions