Skip to content

BUG: Fix groupby sorting on ordered Categoricals (GH25871) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2019

Conversation

kpflugshaupt
Copy link
Owner

@kpflugshaupt kpflugshaupt commented Mar 26, 2019

As documented in pandas-dev#25871, groupby() on an ordered Categorical messes up category order when 'observed=True' is specified.
Specifically, group labels will be ordered by first occurrence (as for an unordered Categorical), but grouped aggregation results will retain the Categorical's order.
The fix is a modified subset of pandas-dev#25173, which fixes a related case, but has not been merged yet.

As documented in pandas-dev#25871, groupby() on an ordered Categorical messes up category order when 'observed=True' is specified. 
Specifically, group labels will be ordered by first occurrence (as for an unordered Categorical), but grouped aggregation results will retain the Categorical's order.
The fix is a modified subset of pandas-dev#25173, which fixes a related case, but has not been merged yet.
@kpflugshaupt kpflugshaupt added bug Something isn't working Categorical groupby labels Mar 26, 2019
@kpflugshaupt kpflugshaupt changed the title BUG: Fix groupby on ordered Categoricals (GH25871) BUG: Fix groupby sorting on ordered Categoricals (GH25871) Mar 28, 2019
Testing all combinations of:
- ordered vs. unordered grouping column
- 'observed' True vs. False
- 'sort' True vs. False
In all cases, result group ordering must be correct. 
The test is built such that the result index labels are equal to aggregation results if all goes well (except for the one unobserved category)
@kpflugshaupt kpflugshaupt marked this pull request as ready for review March 28, 2019 10:54
@kpflugshaupt kpflugshaupt merged commit b265349 into master Mar 28, 2019
kpflugshaupt added a commit that referenced this pull request Mar 28, 2019
kpflugshaupt added a commit that referenced this pull request Mar 28, 2019
* BUG: Fix groupby sorting on ordered Categoricals (GH25871) (#1)

* BUG: Fix groupby on ordered Categoricals (GH25871)

As documented in pandas-dev#25871, groupby() on an ordered Categorical messes up category order when 'observed=True' is specified. 
Specifically, group labels will be ordered by first occurrence (as for an unordered Categorical), but grouped aggregation results will retain the Categorical's order.
The fix is a modified subset of pandas-dev#25173, which fixes a related case, but has not been merged yet.

* BUG: Fix groupby on ordered Categoricals (GH25871)

* new test

* Fix groupby on ordered Categoricals (GH25871)

Testing all combinations of:
- ordered vs. unordered grouping column
- 'observed' True vs. False
- 'sort' True vs. False
In all cases, result group ordering must be correct. 
The test is built such that the result index labels are equal to aggregation results if all goes well (except for the one unobserved category)

* Revert "BUG: Fix groupby sorting on ordered Categoricals (GH25871) (#1)"

This reverts commit b265349.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Categorical groupby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

groupby aggregation on ordered Categorial with 'observed=True' breaks order
1 participant