Conversation
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>
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Co-authored-by: Claude <noreply@anthropic.com>
| ): | ||
| if index.index.is_monotonic_increasing: | ||
| continue | ||
| if index.index.is_monotonic_decreasing and index.index.is_unique: |
There was a problem hiding this comment.
do we need to reverse for decreasing? IIUC the interp will work in any case? and why is_unique?
There was a problem hiding this comment.
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.

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
whats-new.rstapi.rstAI Disclosure