Skip to content

Allow interp along dimensions with a single coordinate value - #11916

Open
VanshajPoonia wants to merge 8 commits into
pydata:mainfrom
VanshajPoonia:interp-single-point-10747
Open

VanshajPoonia wants to merge 8 commits into
pydata:mainfrom
VanshajPoonia:interp-single-point-10747

Conversation

@VanshajPoonia

Copy link
Copy Markdown
Contributor

Description

SciPy's interpolants need at least two points so interp along a dimension of size 1 returned NaN with a RuntimeWarning (linear on 2-D data, as in the issue) or raised a ValueError (slinear, cubic, pchip, akima and others).
interp now handles those dimensions itself, without going through .sel(): it takes the data at the single point, and a new coordinate equal to that point gets it. Any other new coordinate is out of range, so it gets NaN, or the fill_value passed in kwargs (a scalar or a (below, above) pair). Extrapolating (fill_value="extrapolate", extrapolate=True for pchip and akima, or krogh and barycentric, which always extrapolate) repeats the value, since a constant is the only curve through one point. bounds_error=True raises for an off-point coordinate. The other dimensions are interpolated as they were being before.

The check is on the top of missing.interp so it also covers the multi-dimensional interpn path and it stays lazy for dask.
It doesn't touch the parts of missing.py that #11708 and #11885 change. #11885's fast path already skips sources with fewer than two points, and the merge of both passes test_interp.py and test_missing.py.

Checklist

AI Disclosure

VanshajPoonia and others added 3 commits November 19, 2026 00:40
The interpolants need two points or more, so interp returned NaN with a
RuntimeWarning, or raised, along a dimension of size 1. Take the data at
that point for new coordinates equal to it, and treat the others as out
of range.

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

welcome Bot commented Nov 18, 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.

Cubic interpolation can be off by one ulp on some platforms.

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

@atishay-kasliwal atishay-kasliwal 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.

Ran the 15 singleton-interpolation tests (all pass) and a per-component fill regression: ordinary interpolation passes on NumPy and Dask, while the singleton path fails on both.

Comment thread xarray/core/missing.py Outdated
f"A new coordinate of {dim!r} is out of the interpolation "
f"range, which is the single point {x.values[0]}."
)
fill = Variable(new_x.dims, where(new_x.data < point, below, above))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could the fill arrays be broadcast over the remaining data dimensions before combining them with new_x? A tuple of per-y fill arrays works with ordinary interpolation, but this path combines shapes (3,), (2,), (2,) and raises on both NumPy and Dask:

a = xr.DataArray([[2., 5.]], dims=("x", "y"), coords={"x": [6.]})
fill = (np.array([-1., -3.]), np.array([-2., -4.]))
a.interp(x=[4., 6., 8.], kwargs={"fill_value": fill})
# ValueError: operands could not be broadcast together ...

The expected rows are [-1., -3.], [2., 5.], [-2., -4.]; the same call on data [[2., 5.], [2., 5.]] with x=[5., 7.] returns those rows.

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.

Fixed in c99a45f.

  • Array fill values now broadcast against the dimensions that aren't interpolated, the same way scipy does on the normal path.
  • Only a 2-tuple is split into (below, above). A list or array of length 2 is one per-component fill used on both sides, which is also scipy's rule. Before that any 2-item sequence was split.
  • Your example now gives [-1, -3], [2, 5], [-2, -4], the same as the two point data.
  • Tests: three new cases in test_interp_single_point_out_of_range (tuple of arrays, list, array), each on NumPy and Dask with Dask under raise_if_dask_computes.
  • 6 of them fail on the previous head. test_interp + test_missing: 350 pass. pre-commit clean, no new mypy errors.

VanshajPoonia and others added 4 commits November 19, 2026 20:21
An array fill value now lines up with the other dimensions, as scipy does on the normal path, and only a tuple splits it into below and above values.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow interpolating along singleton dimensions

2 participants