Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always force dask arrays to float in missing.interp_func #4771

Merged
merged 5 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,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.
dcherian marked this conversation as resolved.
Show resolved Hide resolved
# 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 @@ -742,7 +749,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