Skip to content

Commit ed304a1

Browse files
authored
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.
1 parent 923ac2b commit ed304a1

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

pandas/core/groupby/grouper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
302302
if observed:
303303
codes = algorithms.unique1d(self.grouper.codes)
304304
codes = codes[codes != -1]
305+
if sort or self.grouper.ordered:
306+
codes = np.sort(codes)
305307
else:
306308
codes = np.arange(len(categories))
307309

0 commit comments

Comments
 (0)