Skip to content

GroupBy.nth includes group key inconsistently #12839

Closed
@sinhrks

Description

@sinhrks

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions