Closed
Description
Code Sample, a copy-pastable example if possible
nth
doesn't inlcude group key as the same as first
and last
.
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [1, 2, 3, 4, 5],
'G': [1, 1, 2, 2, 1]})
g = df.groupby('G')
g.nth(1)
# A B
# G
#1 2 2
#2 4 4
However, calling head
makes the behavior change. Looks to be caused by _set_selection_from_grouper
caches its selection.
g = df.groupby('G')
g.head()
g.nth(1)
# A B G
# G
#1 2 2 1
#2 4 4 2
Expected Output
always as below.
g.nth(1)
# A B
# G
#1 2 2
#2 4 4
output of pd.show_versions()
current master.