Skip to content

Commit

Permalink
Always force dask arrays to float in missing.interp_func (#4771)
Browse files Browse the repository at this point in the history
* scipy.interpolate.interp1d always forces to float.

* Copy type-check from scipy.interpolate.interp1d

* Update missing.py

* Test that pre- and post-compute dtypes matches

* Update test_missing.py
  • Loading branch information
Illviljan authored Jan 12, 2021
1 parent 81ed507 commit 9bb0302
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,13 @@ def interp_func(var, x, new_x, method, kwargs):
# if usefull, re-use localize for each chunk of new_x
localize = (method in ["linear", "nearest"]) and (new_x[0].chunks is not None)

# scipy.interpolate.interp1d always forces to float.
# Use the same check for blockwise as well:
if not issubclass(var.dtype.type, np.inexact):
dtype = np.float_
else:
dtype = var.dtype

return da.blockwise(
_dask_aware_interpnd,
out_ind,
Expand All @@ -738,7 +745,7 @@ def interp_func(var, x, new_x, method, kwargs):
interp_kwargs=kwargs,
localize=localize,
concatenate=True,
dtype=var.dtype,
dtype=dtype,
new_axes=new_axes,
)

Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,20 @@ def test_interpolate_dask_raises_for_invalid_chunk_dim():
da.interpolate_na("time")


@requires_dask
@requires_scipy
@pytest.mark.parametrize("dtype, method", [(int, "linear"), (int, "nearest")])
def test_interpolate_dask_expected_dtype(dtype, method):
da = xr.DataArray(
data=np.array([0, 1], dtype=dtype),
dims=["time"],
coords=dict(time=np.array([0, 1])),
).chunk(dict(time=2))
da = da.interp(time=np.array([0, 0.5, 1, 2]), method=method)

assert da.dtype == da.compute().dtype


@requires_bottleneck
def test_ffill():
da = xr.DataArray(np.array([4, 5, np.nan], dtype=np.float64), dims="x")
Expand Down

0 comments on commit 9bb0302

Please sign in to comment.