Skip to content

fix: normalize negative axis in argmax and argmin - #966

Merged
hameerabbasi merged 1 commit into
pydata:mainfrom
Rodrigo-Palma:fix/argminmax-negative-axis
Oct 7, 2026
Merged

hameerabbasi merged 1 commit into
pydata:mainfrom
Rodrigo-Palma:fix/argminmax-negative-axis

Conversation

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor

What happens

_arg_minmax_common, the backend of sparse.argmax and sparse.argmin, never normalizes the axis, and its bounds check only covers one side (axis >= x.ndim). A negative axis therefore reaches the transpose and reshape logic untouched. The initial list.pop(axis) happens to work for -1, but the final un-transpose does new_transpose.insert(axis, new_transpose.pop(0)), which for -1 inserts before the last element rather than at the end, so the reduced axis ends up in front.

Measured against numpy on a (2, 3) array:

axis=1   numpy [[1], [0]] (2, 1)   sparse [[1], [0]] (2, 1)
axis=-1  numpy [[1], [0]] (2, 1)   sparse [[1, 0]]   (1, 2)     <-- shape and layout wrong

Two more consequences of the same gap:

1-D, axis=-1    numpy 1       sparse ValueError: shape must be an non-negative integer...
2-D, axis=-5    numpy AxisError: axis -5 is out of bounds...
                sparse IndexError: pop index out of range        <-- internal error leaking

Why this is a gap and not a decision

normalize_axis is already imported in this module, and every other axis-taking function in it calls it: concatenate, stack, roll, diagonal, expand_dims and flip. _arg_minmax_common is the only one left out.

The history says the same. The reductions got negative-axis support in #118/#119 (issue #117, "Reduce operations raise error for negative axis values"), and diagonal got it in #955 this month; argmax/argmin arrived later in #614 and were never given the same treatment. The existing tests parametrize axis over [None, 0, 1, 2], so no negative axis was ever exercised.

A half-finished bounds check also points at an oversight rather than intent: it rejects axis >= x.ndim but not axis < -x.ndim.

The change

Two lines: complete the bounds check and normalize the axis. The wording of the out-of-bounds ValueError is kept deliberately, so test_argmax_argmin_constraint keeps passing, including the 0-D case that still lands on the same error.

Tests

  • test_argmax_argmin_negative_axis: 5 (array, axis) pairs in 2-D and 3-D, across keepdims and both functions, compared with np.argmax/np.argmin
  • test_argmax_argmin_negative_axis_1d: the 1-D axis=-1 case that used to raise
  • test_argmax_argmin_constraint: extended with axis=-3 on a 2-D array

Without the change: 10 failed, 46 passed. With it: 56 passed.

Full sparse/numba_backend suite after the change: 6238 passed, 94 xfailed, 63 xpassed, no regressions. ruff check and ruff format --check at v0.16.8 (the revision pinned in .pre-commit-config.yaml) are clean.

`_arg_minmax_common` never normalized the axis, and its bounds check only
covered one side (`axis >= x.ndim`), so a negative axis reached the
transpose and reshape logic untouched. The initial `list.pop(axis)`
happens to work for `-1`, but the final un-transpose does
`new_transpose.insert(axis, new_transpose.pop(0))`, which for `-1`
inserts before the last element instead of at the end. The reduced axis
ends up in front.

Measured against numpy on a (2, 3) array:

    axis=1   numpy [[1], [0]] (2, 1)   sparse [[1], [0]] (2, 1)
    axis=-1  numpy [[1], [0]] (2, 1)   sparse [[1, 0]]   (1, 2)

A 1-D array with `axis=-1` raised `ValueError: shape must be an
non-negative integer...` where numpy returns a value, and `axis=-5` on a
2-D array leaked `IndexError: pop index out of range` instead of a
message naming the bounds.

`normalize_axis` is already imported in this module and every other
axis-taking function in it uses it: `concatenate`, `stack`, `roll`,
`diagonal`, `expand_dims` and `flip`. This was the only one left out,
which also matches the history: the reductions got this treatment in
pydata#118/pydata#119 and `diagonal` in pydata#955, while `argmax`/`argmin` arrived later
in pydata#614 and were never covered. The existing tests parametrize `axis`
over `[None, 0, 1, 2]`, so no negative axis was ever exercised.

The wording of the out-of-bounds `ValueError` is kept so that
`test_argmax_argmin_constraint` still passes, including the 0-D case.
@github-actions github-actions Bot added the fix label Sep 25, 2026
@hameerabbasi
hameerabbasi enabled auto-merge (squash) October 7, 2026 11:35
@hameerabbasi
hameerabbasi merged commit ecd66f3 into pydata:main Oct 7, 2026
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants