Skip to content

BUG: DataFrameGroupBy.value_counts incorrect sorting #55951

Closed
@rhshadrach

Description

@rhshadrach

value_counts does not respect sort=False from groupby:

df = pd.DataFrame({'a': [2, 1, 1], 'b': [3, 4, 3]})
gb = df.groupby('a', sort=False)

result = gb.value_counts()
print(result)
# a  b
# 1  4    1
#    3    1
# 2  3    1
# Name: count, dtype: int64

In the above, the groups should appear with a=2 first and a = 1 second.

If the columns of your DataFrame are integers, the method may sort by these instead.

df = pd.DataFrame({'a': [2, 1, 1], 0: [3, 4, 3]})
gb = df.groupby('a', sort=False)

result = gb.value_counts()
print(result)
# a  0
# 2  3    1
# 1  3    1
#    4    1
# Name: count, dtype: int64

Metadata

Metadata

Assignees

Labels

AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffBugGroupby

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions