From 05c715f44d383ec8492f9f65fb18945f9381c3cf Mon Sep 17 00:00:00 2001 From: Zaccheus Sia Date: Wed, 22 Nov 2023 20:35:34 +0800 Subject: [PATCH 1/4] Use observed=True in groupby --- packages/python/plotly/plotly/express/_core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index b80355c727..037123a37e 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -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 From a4932c66e7bd565b55c92bb5174adfd0cf8ad163 Mon Sep 17 00:00:00 2001 From: Zaccheus Sia Date: Wed, 22 Nov 2023 20:36:55 +0800 Subject: [PATCH 2/4] Test no error raised plotting categorical subset --- .../plotly/tests/test_optional/test_px/test_colors.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py index 85e1dc00bf..b703da4389 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py @@ -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", facet_col="day" + ) From ccc7e89f6d5b10fe4d7da5e1580ad01bca712fee Mon Sep 17 00:00:00 2001 From: Zaccheus Sia Date: Wed, 22 Nov 2023 21:06:39 +0800 Subject: [PATCH 3/4] Typo facet_row to color --- .../plotly/plotly/tests/test_optional/test_px/test_colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py index b703da4389..c57b1b3898 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_colors.py @@ -59,5 +59,5 @@ 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", facet_col="day" + df[df.day != df.day.cat.categories[0]], x="total_bill", y="tip", color="day" ) From e77e45f58d7dd43cfa0b7f17c09448392db214ed Mon Sep 17 00:00:00 2001 From: Zaccheus Sia Date: Thu, 23 Nov 2023 09:34:59 +0800 Subject: [PATCH 4/4] changelog entry for #4437 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f34484ea6..596e041846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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