Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[ ] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
pd.__version__
df = pd.DataFrame({"x": [1, 2, 2, 3, 3], "y": [10, 20, 30, 40, 50]})
gb = df.groupby("x")["y"]
gb.nth(0)
Issue Description
With the development version, you get:
'2.0.0.dev0+643.ge41b6d7827'
0 10
1 20
3 40
Name: y, dtype: int64
With pandas 1.5.1, you get:
x
1 10
2 20
3 40
Name: y, dtype: int64
Expected Behavior
Should be the 1.5.1 behavior.
Note that the index name is missing as well as the values shown for the index don't correspond to the group values (i.e., the values of 'x'
Installed Versions
Comment From: MarcoGorelli
Nice - out of interest, how did you spot this?
Am running git bisect on this https://www.kaggle.com/code/marcogorelli/pandas-regression-example?scriptVersionId=110720326
Comment From: MarcoGorelli
This changed in https://github.com/pandas-dev/pandas/pull/49262 , and looks like it was intentional:
in particular, the result index no longer contains the groupers but rather is filtered from the original index of the input
should be OK to close then?
Comment From: MarcoGorelli
This is now the same as
In [8]: df.groupby('x')['y'].nth(0)
Out[8]:
0 10
1 20
3 40
Name: y, dtype: int64
In [9]: df.groupby('x')['y'].head(1)
Out[9]:
0 10
1 20
3 40
Name: y, dtype: int64
Closing for now then, but cc @rhshadrach in case this is an issue