Skip to content

BUG: fix sorting with observed=True in groupby #25173

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Bug Fixes

**Reshaping**

-
- Fixed bug in :meth:`DataFrame.groupby` where using ``observed=True`` didn't sort properly (:issue:`25167`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to 0.25.0

-
-

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/groupby/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def recode_for_groupby(c, sort, observed):
unique_codes = unique1d(c.codes)

take_codes = unique_codes[unique_codes != -1]
if c.ordered:
take_codes = np.sort(take_codes)
take_codes = np.sort(take_codes)

# we recode according to the uniques
categories = c.categories.take(take_codes)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
if observed:
codes = algorithms.unique1d(self.grouper.codes)
codes = codes[codes != -1]
if sort:
codes = np.sort(codes)
else:
codes = np.arange(len(categories))

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ def test_observed(observed):
expected = groups2.agg('mean').reset_index()
tm.assert_frame_equal(result, expected)

# GH 25167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this a separate test, parameterize by sort as well

df = pd.DataFrame({'A': pd.Categorical(['b', 'a']), 'B': [1, 2]})
expected = pd.DataFrame([2, 1],
index=pd.CategoricalIndex(['a', 'b'], name='A'),
columns=['B'])
result = df.groupby('A', observed=observed).sum()
tm.assert_frame_equal(result, expected)


def test_observed_codes_remap(observed):
d = {'C1': [3, 3, 4, 5], 'C2': [1, 2, 3, 4], 'C3': [10, 100, 200, 34]}
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def test_groupby_categorical_index_and_columns(self, observed):
# if we are not-observed we undergo a reindex
# so need to adjust the output as our expected sets us up
# to be non-observed
expected_columns = CategoricalIndex(['A', 'B'],
expected_columns = CategoricalIndex(['B', 'A'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected_columns should have ['B', 'A'], because ordered=True.

categories=categories,
ordered=True)
else:
Expand Down