Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
series = pd.Series([5, 7], index=['b', 'a'])
assert list(series.groupby(level=0).groups.keys()) == list(series.groupby(pd.Grouper(level=0)).groups.keys())
Issue Description
series.groupby(level=0) and series.groupby(pd.Grouper(level=0)) produce different orders
Expected Behavior
The result of series.groupby(level=0) should be sorted just as the result of series.groupby(pd.Grouper(level=0)) is.
Installed Versions
Comment From: geekalaa
The issue is caused by inconsistent default values for the sort parameter:
- pd.Grouper.init has sort: bool = False
- Series.groupby has sort: bool = True
This explains why: - series.groupby(level=0) returns keys in sorted order: ['a', 'b'] (uses sort=True default) - series.groupby(pd.Grouper(level=0)) returns keys in original index order: ['b', 'a'] (uses sort=False default)
Comment From: rhshadrach
Thanks for the report. I do think we should align these to default to sort=True
. @jbrockmendel - do you agree?
Comment From: jbrockmendel
agreed i would expect these to match, no real preference on what the default should be.
Comment From: rhshadrach
Main reason I would change Grouper is that I'd guess it sees a lot less use.