Skip to content

BUG: SeriesGroupBy.value_counts sorts when sort=False #50482

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
rhshadrach opened this issue Dec 29, 2022 · 0 comments · Fixed by #50548
Closed

BUG: SeriesGroupBy.value_counts sorts when sort=False #50482

rhshadrach opened this issue Dec 29, 2022 · 0 comments · Fixed by #50548
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Groupby
Milestone

Comments

@rhshadrach
Copy link
Member

In the 2nd example, the column gender is reverse-lexicographically sorted (male then female).

df = DataFrame(
    {
        "gender": ["male", "male", "female", "male", "female", "male"],
        "education": ["low", "medium", "high", "low", "high", "low"],
        "country": ["US", "FR", "US", "FR", "FR", "FR"],
    }
)
gb = df.groupby(["country", "gender"], as_index=True, sort=False)
result = gb.value_counts(sort=False)
print(result)
# country  gender  education
# US       male    low          1
# FR       male    medium       1
# US       female  high         1
# FR       male    low          2
#          female  high         1
# dtype: int64

gb2 = df.groupby(["country", "gender"], as_index=True, sort=False)["education"]
result2 = gb2.value_counts(sort=False)
print(result2)
# country  gender  education
# US       male    low          1
# FR       male    low          2
#                  medium       1
# US       female  high         1
# FR       female  high         1
# Name: education, dtype: int64
@rhshadrach rhshadrach added Bug Groupby Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff labels Dec 29, 2022
@rhshadrach rhshadrach self-assigned this Dec 29, 2022
@rhshadrach rhshadrach added this to the 2.0 milestone Jan 11, 2023
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

Successfully merging a pull request may close this issue.

1 participant