Skip to content

Commit 63b9ac5

Browse files
authored
Merge pull request #4437 from arcanaxion/groupby-categorical-futurewarning
Fix KeyError when using column of pd.Categorical dtype with unobserved categories
2 parents d4a7c32 + e77e45f commit 63b9ac5

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
88
- Ensure scatter `mode` is deterministic from `px` [[#4429](https://github.com/plotly/plotly.py/pull/4429)]
99
- Fix issue with creating dendrogram in subplots [[#4411](https://github.com/plotly/plotly.py/pull/4411)],
1010
- Fix issue with px.line not accepting "spline" line shape [[#2812](https://github.com/plotly/plotly.py/issues/2812)]
11+
- Fix KeyError when using column of `pd.Categorical` dtype with unobserved categories [[#4437](https://github.com/plotly/plotly.py/pull/4437)]
1112

1213
## [5.18.0] - 2023-10-25
1314

Diff for: packages/python/plotly/plotly/express/_core.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,9 @@ def get_groups_and_orders(args, grouper):
20422042
groups = {tuple(single_group_name): df}
20432043
else:
20442044
required_grouper = [g for g in grouper if g != one_group]
2045-
grouped = df.groupby(required_grouper, sort=False) # skip one_group groupers
2045+
grouped = df.groupby(
2046+
required_grouper, sort=False, observed=True
2047+
) # skip one_group groupers
20462048
group_indices = grouped.indices
20472049
sorted_group_names = [
20482050
g if len(required_grouper) != 1 else (g,) for g in group_indices

Diff for: packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py

+8
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ def test_r_colorscales():
5353
assert scale.replace("_r", "") in scale_names
5454
else:
5555
assert scale + "_r" in scale_names
56+
57+
58+
def test_color_categorical_dtype():
59+
df = px.data.tips()
60+
df["day"] = df["day"].astype("category")
61+
px.scatter(
62+
df[df.day != df.day.cat.categories[0]], x="total_bill", y="tip", color="day"
63+
)

0 commit comments

Comments
 (0)