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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Ensure scatter `mode` is deterministic from `px` [[#4429](https://github.com/plotly/plotly.py/pull/4429)]
- Fix issue with creating dendrogram in subplots [[#4411](https://github.com/plotly/plotly.py/pull/4411)],
- Fix issue with px.line not accepting "spline" line shape [[#2812](https://github.com/plotly/plotly.py/issues/2812)]
- Fix KeyError when using column of `pd.Categorical` dtype with unobserved categories [[#4437](https://github.com/plotly/plotly.py/pull/4437)]

## [5.18.0] - 2023-10-25

Expand Down
4 changes: 3 additions & 1 deletion packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,9 @@ def get_groups_and_orders(args, grouper):
groups = {tuple(single_group_name): df}
else:
required_grouper = [g for g in grouper if g != one_group]
grouped = df.groupby(required_grouper, sort=False) # skip one_group groupers
grouped = df.groupby(
required_grouper, sort=False, observed=True
) # skip one_group groupers
group_indices = grouped.indices
sorted_group_names = [
g if len(required_grouper) != 1 else (g,) for g in group_indices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ def test_r_colorscales():
assert scale.replace("_r", "") in scale_names
else:
assert scale + "_r" in scale_names


def test_color_categorical_dtype():
df = px.data.tips()
df["day"] = df["day"].astype("category")
px.scatter(
df[df.day != df.day.cat.categories[0]], x="total_bill", y="tip", color="day"
)