Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: groupby with as_index=False drops groupings when names collide #58337

Open
rhshadrach opened this issue Apr 20, 2024 · 1 comment
Open
Assignees

Comments

@rhshadrach
Copy link
Member

rhshadrach commented Apr 20, 2024

I think this is really and edge case because in order to get this behavior you need to group by an external Series with the same name as one of the columns.

df = pd.DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})

print(df.groupby(pd.Series([6, 6, 7], name="a"), as_index=False).sum())
#    a  b
# 0  2  7
# 1  2  5
print(df.groupby(pd.Series([6, 6, 7], name="a"), as_index=True).sum().reset_index())
# ValueError: cannot insert a, already exists

I think users expect as_index=False to behave the same as calling reset_index() after using as_index=True. It is not good behavior that we silently drop the groupings when as_index=False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment