Skip to content

BUG: groupby drops columns if the column name is the same as the grouper #8112

Closed
@behzadnouri

Description

@behzadnouri
>>> df = pd.DataFrame(np.random.randint(1, 9, (2, 3)), columns=['jim', 'joe', 'jolie'])
>>> df
   jim  joe  jolie
0    4    7      8
1    7    8      3
>>> ts = df['joe'] * 0
>>> ts.name
'joe'
>>> gr = df.groupby(ts)
>>> gr.nth(0)  # this invokes _set_selection_from_grouper internally
     jim  jolie
joe
7      4      8
>>> gr.apply(sum)  # joe column is gone
     jim  jolie
joe
0     11     11

whereas:

>>> df.groupby(ts).apply(sum)
     jim  joe  jolie
joe
0     11   15     11

What happens is that this line removes the column from selection if it has the same name as the grouper.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions