Registry Mirror
Skip to content

Conversation

@bhaskargurram-ai

@bhaskargurram-ai bhaskargurram-ai commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Dataset.interp with assume_sorted=False called sortby on all interpolated dimensions, which indexes with an integer array and copies the data even when the coordinate is already increasing. Now increasing coordinates and dimensions without a coordinate are left alone, strictly decreasing coordinates are reversed with a slice, and only the remaining dimensions go through sortby.

Closes #9758

Description

Checklist

  • Closes #xxxx
  • 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.
    • I take responsibility for any AI-generated content in my PR. Tools: {e.g., Claude, Codex, GitHub Copilot, ChatGPT, etc.}

Dataset.interp with assume_sorted=False called sortby on all
interpolated dimensions, which indexes with an integer array and copies
the data even when the coordinate is already increasing. Now increasing
coordinates and dimensions without a coordinate are left alone,
strictly decreasing coordinates are reversed with a slice, and only the
remaining dimensions go through sortby.

Closes pydata#9758

Co-authored-by: Claude <noreply@anthropic.com>
@welcome

welcome Bot commented Oct 1, 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.

Co-authored-by: Claude <noreply@anthropic.com>
Comment thread xarray/core/dataset.py
):
if index.index.is_monotonic_increasing:
continue
if index.index.is_monotonic_decreasing and index.index.is_unique:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need to reverse for decreasing? IIUC the interp will work in any case? and why is_unique?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I checked both:

Yes, the reverse is needed. Passing a decreasing coordinate straight through breaks most methods:
linear and nearest go through missing._localize, which builds slice(imin - 2, imax + 2) assuming ascending values, so the slice is empty and scipy fails on zero points;
the scipy spline methods (zero/slinear/quadratic/cubic/pchip/akima/makima/polynomial) raise "x must be strictly increasing";
the vectorized linear/nearest path fails the same way.

Only barycentric, krogh and some interpn methods cope.

is_unique is about ties. sortby uses np.lexsort, which is stable, so tied coordinate values keep their order, while a reversed slice flips them. With x = [4, 3, 3, 2, 1], krogh gives different values after reversing than after sortby (the other methods reject the repeated points either way). With the uniqueness check, the reversal is exactly what sortby would have done, and a decreasing coordinate with ties still goes through sortby, so results are unchanged from main.

I added a comment to _sort_for_interp and a test for the tie case in a29316a (the test fails if the is_unique check is removed).

A decreasing coordinate still has to be flipped, because _localize and the
scipy interpolators assume increasing values. Reversing with a slice matches
sortby's stable sort only when there are no ties, so a tied decreasing
coordinate keeps going through sortby; the new test fails if that guard is
dropped.
@dcherian
dcherian enabled auto-merge (squash) October 2, 2026 16:36
@dcherian dcherian added the plan to merge Final call for comments label Oct 2, 2026
@dcherian
dcherian merged commit 04c85cc into pydata:main Oct 2, 2026
51 of 58 checks passed
@welcome

welcome Bot commented Oct 2, 2026

Copy link
Copy Markdown

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

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

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf improvement for interp: set assume_sorted automatically

2 participants