Repository navigation
fix: normalize negative axis in argmax and argmin - #966
Merged
hameerabbasi merged 1 commit intoOct 7, 2026
Merged
Conversation
`_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.
hameerabbasi
approved these changes
Oct 7, 2026
hameerabbasi
enabled auto-merge (squash)
October 7, 2026 11:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happens
_arg_minmax_common, the backend ofsparse.argmaxandsparse.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 initiallist.pop(axis)happens to work for-1, but the final un-transpose doesnew_transpose.insert(axis, new_transpose.pop(0)), which for-1inserts 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:Two more consequences of the same gap:
Why this is a gap and not a decision
normalize_axisis already imported in this module, and every other axis-taking function in it calls it:concatenate,stack,roll,diagonal,expand_dimsandflip._arg_minmax_commonis 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
diagonalgot it in #955 this month;argmax/argminarrived later in #614 and were never given the same treatment. The existing tests parametrizeaxisover[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.ndimbut notaxis < -x.ndim.The change
Two lines: complete the bounds check and normalize the axis. The wording of the out-of-bounds
ValueErroris kept deliberately, sotest_argmax_argmin_constraintkeeps 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, acrosskeepdimsand both functions, compared withnp.argmax/np.argmintest_argmax_argmin_negative_axis_1d: the 1-Daxis=-1case that used to raisetest_argmax_argmin_constraint: extended withaxis=-3on a 2-D arrayWithout the change: 10 failed, 46 passed. With it: 56 passed.
Full
sparse/numba_backendsuite after the change:6238 passed, 94 xfailed, 63 xpassed, no regressions.ruff checkandruff format --checkat v0.16.8 (the revision pinned in.pre-commit-config.yaml) are clean.