Skip to content

Commit

Permalink
Merge pull request #4437 from arcanaxion/groupby-categorical-futurewa…
Browse files Browse the repository at this point in the history
…rning

Fix KeyError when using column of pd.Categorical dtype with unobserved categories
  • Loading branch information
alexcjohnson authored Nov 23, 2023
2 parents d4a7c32 + e77e45f commit 63b9ac5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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"
)

0 comments on commit 63b9ac5

Please sign in to comment.