Fix categorical hue data swapped in split violinplot#3966
Open
genrichez wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 causesiter_datato 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=Falsetoobserved=Truein theiter_datagroupby call. This is safe because:observed=Falsewas only added in 9049ddd to silence a pandas FutureWarning - the original code used the default (which wasTruein older pandas).True.iter_dataalready handles missing groups via the existingKeyErrorcatch, 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.