Skip to content

Fix categorical hue data swapped in split violinplot#3966

Open
genrichez wants to merge 1 commit into
mwaskom:masterfrom
genrichez:fix/categorical-hue-order-groupby
Open

Fix categorical hue data swapped in split violinplot#3966
genrichez wants to merge 1 commit into
mwaskom:masterfrom
genrichez:fix/categorical-hue-order-groupby

Conversation

@genrichez

Copy link
Copy Markdown

Fixes #3893.

With pandas 3.0 and a CategoricalDtype hue column, groupby(..., sort=False, observed=False).get_group(key) matches groups by category code rather than by label value. This causes iter_data to yield data belonging to the wrong hue group when the first-appearance order in the data differs from the category order - resulting in swapped violins in split violin plots.

The fix changes observed=False to observed=True in the iter_data groupby call. This is safe because:

  1. The observed=False was only added in 9049ddd to silence a pandas FutureWarning - the original code used the default (which was True in older pandas).
  2. Seaborn's minimum pandas is now 2.2, where the default is True.
  3. iter_data already handles missing groups via the existing KeyError catch, so unobserved category combinations still produce empty DataFrames as expected.

Includes a regression test that verifies the correct hue group data is assigned to each violin half.

With pandas 3.0 and CategoricalDtype hue columns, groupby(...,
sort=False, observed=False).get_group(key) matches groups by category
code rather than by label value. This causes iter_data to yield data
belonging to the wrong hue group when the first-appearance order in the
data differs from the category order.

Changing observed=False to observed=True fixes the lookup. Unobserved
category combinations are still handled correctly because iter_data
already catches KeyError and returns an empty DataFrame for missing
groups.

Closes mwaskom#3893
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hue_order breaks with pandas StringArray

1 participant