diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e5eb4680878..ccc779105a0 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -98,6 +98,8 @@ Bug fixes :py:meth:`Dataset.to_zarr` (:issue:`4783`, :pull:`4795`). By `Julien Seguinot `_. - Add :py:meth:`Dataset.drop_isel` and :py:meth:`DataArray.drop_isel` (:issue:`4658`, :pull:`4819`). By `Daniel Mesejo `_. +- Ensure that :py:meth:`Dataset.interp` raises ``ValueError`` when interpolating outside coordinate range and ``bounds_error=True`` (:issue:`4854`, :pull:`4855`). + By `Leif Denby `_. - Fix time encoding bug associated with using cftime versions greater than 1.4.0 with xarray (:issue:`4870`, :pull:`4871`). By `Spencer Clark `_. diff --git a/xarray/core/missing.py b/xarray/core/missing.py index 695affa84c1..e6dd8b537a0 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -154,7 +154,7 @@ def __init__( yi, kind=self.method, fill_value=fill_value, - bounds_error=False, + bounds_error=bounds_error, assume_sorted=assume_sorted, copy=copy, **self.cons_kwargs, diff --git a/xarray/tests/test_interp.py b/xarray/tests/test_interp.py index 20d5fb12a62..cdfc46bbedf 100644 --- a/xarray/tests/test_interp.py +++ b/xarray/tests/test_interp.py @@ -866,3 +866,18 @@ def test_interpolate_chunk_advanced(method): z = z.chunk(3) actual = da.interp(t=0.5, x=x, y=y, z=z, kwargs=kwargs, method=method) assert_identical(actual, expected) + + +@requires_scipy +def test_interp1d_bounds_error(): + """Ensure exception on bounds error is raised if requested""" + da = xr.DataArray( + np.sin(0.3 * np.arange(4)), + [("time", np.arange(4))], + ) + + with pytest.raises(ValueError): + da.interp(time=3.5, kwargs=dict(bounds_error=True)) + + # default is to fill with nans, so this should pass + da.interp(time=3.5)