BUG: Fix groupby sorting on ordered Categoricals (GH25871)
State: closed

As documented in #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 #25173, which fixes a related case, but has not been merged yet.

- [x] closes #25871
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:

Comments:
Hello @kpflugshaupt! Thanks for opening this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:



* In the file [`pandas/tests/groupby/test_categorical.py`](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py):

> [Line 456:36](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L456): [E241](https://duckduckgo.com/?q=pep8%20E241) multiple spaces after ','
> [Line 458:33](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L458): [E241](https://duckduckgo.com/?q=pep8%20E241) multiple spaces after ','
> [Line 460:60](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L460): [W291](https://duckduckgo.com/?q=pep8%20W291) trailing whitespace
> [Line 461:80](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L461): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (118 > 79 characters)
> [Line 462:80](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L462): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (109 > 79 characters)
> [Line 463:20](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L463): [E211](https://duckduckgo.com/?q=pep8%20E211) whitespace before '('
> [Line 464:7](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L464): [E221](https://duckduckgo.com/?q=pep8%20E221) multiple spaces before operator
> [Line 467:80](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L467): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (80 > 79 characters)
> [Line 468:1](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L468): [W293](https://duckduckgo.com/?q=pep8%20W293) blank line contains whitespace
> [Line 469:80](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L469): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (82 > 79 characters)
> [Line 470:80](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L470): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (82 > 79 characters)
> [Line 472:9](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L472): [E221](https://duckduckgo.com/?q=pep8%20E221) multiple spaces before operator
> [Line 475:33](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L475): [W291](https://duckduckgo.com/?q=pep8%20W291) trailing whitespace
> [Line 477:1](https://github.com/pandas-dev/pandas/blob/2c2500b04db9d6800a4d73a350efee0eb4984f2d/pandas/tests/groupby/test_categorical.py#L477): [W293](https://duckduckgo.com/?q=pep8%20W293) blank line contains whitespace


Will fix the PEP 8 issues ASAP (sorry -- did not check the test source...)