Skip to content

Bug: groupby(..., observed=True) doesn't respect sort key #27369

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

Closed
topper-123 opened this issue Jul 12, 2019 · 2 comments
Closed

Bug: groupby(..., observed=True) doesn't respect sort key #27369

topper-123 opened this issue Jul 12, 2019 · 2 comments
Labels
Bug Categorical Categorical Data Type Groupby
Milestone

Comments

@topper-123
Copy link
Contributor

topper-123 commented Jul 12, 2019

df.groupby(..., observed=True) behaves inconsistently for unordered Categoricals:

>>> df = pd.DataFrame({'A': pd.Categorical(['b', 'a']), 'B': [1,2]}) 
>>> df.groupby('A', observed=False).sum()  # ok
   B
A
a  2
b  1
>>> df.groupby('A', observed=True).sum()  # not ok
   B
A
b  1
a  2

Because the sort parameter in the groupby is implicitly True, the second result should be the same as if sort_index had been called:

>>> df.groupby('A', observed=True).sum().sort_index()  # ok result, but shouldn't be needed
   B
A
a  2
b  1

My guess is that somewhere there is missing a if sort: obj.sort_index() block or similar.

@topper-123 topper-123 changed the title Bug: groupby(..., observed=True) doesn't respect sort key work Bug: groupby(..., observed=True) doesn't respect sort key Jul 12, 2019
@mroeschke mroeschke added the Categorical Categorical Data Type label Jul 10, 2021
@rhshadrach rhshadrach added this to the 2.0 milestone Dec 6, 2022
@rhshadrach
Copy link
Member

This is fixed on main and is tested in test_category_order_reducer.

@topper-123
Copy link
Contributor Author

That’s really great. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants