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

Fix KeyError when using column of pd.Categorical dtype with unobserved categories #4437

Merged

Conversation

arcanaxion
Copy link
Contributor

This PR passes observed=True to omit categorical values with no observations in the dataset. Fixes #4274 and #4433.

Also silences the FutureWarning from pandas-dev/pandas#43999 since pandas 2.1 that the default of observed=False will be changed to True in a future version.

Following code raises KeyError: 'Fri' before this fix:

import plotly.express as px
df = px.data.tips()
df["day"] = df["day"].astype("category")
px.scatter(
    df[df.day != "Fri"],
    x="total_bill",
    y="tip",
    facet_col="day",
)

Copy link
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃 Thanks @arcanaxion, your fix looks great, and thanks so much for the clean & clear test.

CI failure looks flaky, I've rerun it. Would you add a changelog entry, then we can merge?

@arcanaxion
Copy link
Contributor Author

Thanks @alexcjohnson! Just added a changelog entry. Will this do?

@alexcjohnson alexcjohnson merged commit 63b9ac5 into plotly:master Nov 23, 2023
4 checks passed
@arcanaxion arcanaxion deleted the groupby-categorical-futurewarning branch November 24, 2023 00:25
@Coding-with-Adam
Copy link
Contributor

Coding-with-Adam commented Nov 24, 2023

Thank you for this PR @arcanaxion
The community's support in building new features or fixing bugs is of great importance and very much appreciated. If there is anything else that catches your eye, let us know.

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

Successfully merging this pull request may close these issues.

Plotly Express error with subset categorical pandas column
3 participants