Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Infer categorical order from ordered pandas categoricals #158

Open
joelostblom opened this issue Nov 10, 2019 · 1 comment
Open

Infer categorical order from ordered pandas categoricals #158

joelostblom opened this issue Nov 10, 2019 · 1 comment

Comments

@joelostblom
Copy link

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?

@bdwolfe
Copy link

bdwolfe commented Mar 17, 2023

Yes, this would be very useful

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants