Skip to content

BUG: idxmin/idxmax with skipna=False returned wrong results (GH-56903)#65910

Draft
jbrockmendel wants to merge 2 commits into
pandas-dev:mainfrom
jbrockmendel:split-65597
Draft

BUG: idxmin/idxmax with skipna=False returned wrong results (GH-56903)#65910
jbrockmendel wants to merge 2 commits into
pandas-dev:mainfrom
jbrockmendel:split-65597

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Splits the bug fix out of GH-65597 (which becomes a pure performance PR).

group_idxmin_idxmax initializes its output to the -1 all-NA sentinel, and with skipna=False the guard that locks a group to NA could not tell that initial sentinel apart from a group that had genuinely encountered an NA — so it skipped every group and returned the sentinel for all of them. The fix gates the lock on seen[lab, j], which is only set once a group has been visited.

Two user-visible bugs, both fixed by the one kernel change:

  • DataFrame.idxmax / DataFrame.idxmin with axis=1 and skipna=False on extension-array dtypes (e.g. BooleanDtype, nullable integer/float, ArrowDtype) returned incorrect column labels — the PERF: pd.BooleanDtype in row operations is still very slow #56903 manifestation.
  • DataFrameGroupBy.idxmax / idxmin (and Series variants) with skipna=False returned incorrect results when the input had no NA values (the groupby wrapper raises whenever any NA is present, so the broken kernel path was only reached on all-non-NA input).

GH-65597 still needs its own skipna=False raise guard (the direct _groupby_op call bypasses the groupby-level raise) but can drop the skipna = True workaround once this lands.

Test plan

  • pandas/tests/frame/test_reductions.py (axis=1 EA correctness vs numpy + all-NA-row raising)
  • pandas/tests/groupby/test_reductions.py (skipna=False with no NA values)
  • pandas/tests/groupby, pandas/tests/arrays/masked, pandas/tests/extension — no regressions
  • pre-commit run on changed files

🤖 Generated with Claude Code

group_idxmin_idxmax initializes its output to the -1 all-NA sentinel and,
with skipna=False, could not distinguish that initial state from a group
that had actually encountered an NA. It therefore skipped every group and
returned the sentinel for all of them. Gate the lock on `seen` so the
sentinel only short-circuits once a real NA has been seen.

This fixes DataFrame.idxmax/idxmin(axis=1, skipna=False) on extension-array
dtypes and groupby idxmax/idxmin(skipna=False) on inputs with no NA values.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Groupby Reduction Operations sum, mean, min, max, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant