Skip to content

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

Open
Oscar-man-shrestha wants to merge 4 commits into
pandas-dev:mainfrom
Oscar-man-shrestha:codex/groupby-external-name-collision
Open

Oscar-man-shrestha wants to merge 4 commits into
pandas-dev:mainfrom
Oscar-man-shrestha:codex/groupby-external-name-collision

Conversation

@Oscar-man-shrestha

@Oscar-man-shrestha Oscar-man-shrestha commented Nov 8, 2026 •

Copy link
Copy Markdown

What happened to labels 6 and 7

The external grouping Series is named a and labels the rows [6, 6, 7]. The DataFrame also has a data column named a with values [1, 1, 2], so the sums are 2 for group 6 and 2 for group 7. With as_index=False, pandas skipped inserting the external group labels because the result already had a column named a holding [2, 2]. The old output showed a = [2, 2] and omitted the group labels.

Why ValueError matches reset_index() here

With as_index=True, the labels sit in an index named a next to a data column named a. Calling reset_index() raises ValueError: cannot insert a, already exists. Both sets of values can be kept if the grouping key is renamed. With the name left as a, though, pandas would have to invent a new name or produce duplicate a columns. Raising the same error from as_index=False lets the caller choose the name instead of having pandas guess.

Changes

  • _insert_inaxis_grouper now raises ValueError when a named external grouper collides with a result column, instead of skipping the group labels.

  • DataFrameGroupBy.aggregate with a callable returns the result of _python_agg_general directly, since that already inserts the grouping columns; previously they would be inserted twice.

  • Behavior change: test_groupby_as_index_agg grouped by an external Series named jim that collides with a column. mean, max, count, idxmax and all now raise ValueError there, matching reset_index(). cumsum is a transform, inserts no grouping column, and is unchanged.

  • Tests cover the collision, the renamed key (external.rename("group") keeps [6, 7] next to the sums), and a callable aggregation inserting the key once.

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

  • Tests added and passed if fixing a bug or adding a new feature

  • All code checks passed.

  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

  • I have reviewed and followed all the contribution guidelines

Check exactly one of the following, per the automated contributions policy:

  • I did not use AI to develop this pull request.
  • I used AI to develop this pull request. I prompted it to follow AGENTS.md, I have reviewed and understood every change, and I have described above how I used it and exactly which tool, model version, and effort setting — e.g. claude opus 4.8 (xhigh), not just claude.

@github-actions github-actions Bot added the Awaiting Review PR in the maintainers' court; exempt from staleness label Nov 9, 2026

This branch has not been deployed

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

Labels

Awaiting Review PR in the maintainers' court; exempt from staleness

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant