Skip to content
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

BUG: DataFrameGroupBy.value_counts incorrect sorting #55951

Closed
rhshadrach opened this issue Nov 13, 2023 · 1 comment
Closed

BUG: DataFrameGroupBy.value_counts incorrect sorting #55951

rhshadrach opened this issue Nov 13, 2023 · 1 comment
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Groupby

Comments

@rhshadrach
Copy link
Member

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
@rhshadrach rhshadrach added Bug Groupby Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff labels Nov 13, 2023
@rhshadrach rhshadrach self-assigned this Nov 13, 2023
@rhshadrach
Copy link
Member Author

Closed by #56016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Groupby
Projects
None yet
Development

No branches or pull requests

1 participant