Skip to content

Fix negative indices in rolling argmin/argmax at array edges - #11900

Open
ptlnextdoor wants to merge 1 commit into
pydata:mainfrom
ptlnextdoor:fix/oss-xarray-11899
Open

ptlnextdoor wants to merge 1 commit into
pydata:mainfrom
ptlnextdoor:fix/oss-xarray-11899

Conversation

@ptlnextdoor

Copy link
Copy Markdown

Description

Rolling and cumulative argmin/argmax returned negative indices at the array edges when a value equals the padding sentinel. This is a regression from #11696 and is on main only.

Edge windows are padded with True (bool), inf (float) or the largest integer. #11696 subtracts the pad count from the index. If a real value ties with the sentinel, argmin picks the padding and the index goes negative. Example:

da = xr.DataArray([True, True, False, True], dims="t")
da.rolling(t=3, min_periods=1).argmin().values  # [-2. -1.  2.  1.]

The result is now [0, 0, 2, 1], as np.argmin gives on each window. The fix clips the index at 0, the first value of the window inside the array. If the padding wins, all real values in the window tie with it, so 0 is correct.

This PR fixes only item 1 of the issue. The groupby items (2, 3) and the bottleneck tie difference (4) have different causes and are not changed.

Tests: new test_rolling_argminmax_ties_with_padding (bool, float and int64 cases; rolling and cumulative; numbagg on and off). It fails on main and passes now. The whole xarray/tests/test_rolling.py passes (3675 passed, 34 skipped). ruff check and ruff format are clean on the changed files. I did not run the full test suite.

Checklist

Fixes #11899

  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
      Tools: Claude (Anthropic)

AI disclosure: this PR was prepared with AI assistance (Claude, Anthropic).

When a real value equals the padding sentinel (True, inf, or the largest
integer for argmin), the reduction picks the padding. Subtracting the
padding count then gave a negative index. Clip the index at 0, the first
value of the window inside the array.

Co-authored-by: Claude <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 17, 2026 05:12
@welcome

welcome Bot commented Nov 17, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rolling argmin/argmax return negative indices on main (regression from #11696); groupby results also differ by compute path

2 participants