Skip to content

Fix single item nanmax and nanmin reductions #8484

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

Merged
merged 1 commit into from
Dec 14, 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
4 changes: 2 additions & 2 deletions dask/array/reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def nanmin(a, axis=None, keepdims=False, split_every=None, out=None):


def _nanmin_skip(x_chunk, axis, keepdims):
if len(x_chunk):
if x_chunk.size > 0:
return np.nanmin(x_chunk, axis=axis, keepdims=keepdims)
else:
return asarray_safe(
Expand Down Expand Up @@ -563,7 +563,7 @@ def nanmax(a, axis=None, keepdims=False, split_every=None, out=None):


def _nanmax_skip(x_chunk, axis, keepdims):
if len(x_chunk):
if x_chunk.size > 0:
return np.nanmax(x_chunk, axis=axis, keepdims=keepdims)
else:
return asarray_safe(
Expand Down
32 changes: 32 additions & 0 deletions dask/array/tests/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ def test_numel(dtype, keepdims):
)


def reduction_0d_test(da_func, darr, np_func, narr):
expected = np_func(narr)
actual = da_func(darr)

assert_eq(actual, expected)
assert_eq(da_func(narr), expected) # Ensure Dask reductions work with NumPy arrays
assert actual.size == 1


def test_reductions_0D():
x = np.int_(3) # np.int_ has a dtype attribute, np.int does not.
a = da.from_array(x, chunks=(1,))

reduction_0d_test(da.sum, a, np.sum, x)
reduction_0d_test(da.prod, a, np.prod, x)
reduction_0d_test(da.mean, a, np.mean, x)
reduction_0d_test(da.var, a, np.var, x)
reduction_0d_test(da.std, a, np.std, x)
reduction_0d_test(da.min, a, np.min, x)
reduction_0d_test(da.max, a, np.max, x)
reduction_0d_test(da.any, a, np.any, x)
reduction_0d_test(da.all, a, np.all, x)

reduction_0d_test(da.nansum, a, np.nansum, x)
reduction_0d_test(da.nanprod, a, np.nanprod, x)
reduction_0d_test(da.nanmean, a, np.mean, x)
reduction_0d_test(da.nanvar, a, np.var, x)
reduction_0d_test(da.nanstd, a, np.std, x)
reduction_0d_test(da.nanmin, a, np.nanmin, x)
reduction_0d_test(da.nanmax, a, np.nanmax, x)


def reduction_1d_test(da_func, darr, np_func, narr, use_dtype=True, split_every=True):
assert_eq(da_func(darr), np_func(narr))
assert_eq(
Expand Down