Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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

Merged

Conversation

@arcanaxion
Copy link
Contributor

@arcanaxion arcanaxion commented Nov 22, 2023

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",
)

Coding-with-Adam reacted with rocket emoji
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?

Copy link
Contributor Author

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

alexcjohnson and Coding-with-Adam reacted with hooray emoji

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

Coding-with-Adam commented Nov 24, 2023
edited
Loading

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

Reviewers

@alexcjohnson alexcjohnson alexcjohnson approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Plotly Express error with subset categorical pandas column

AltStyle によって変換されたページ (->オリジナル) /