diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index af5c415cdce..b30ba4c3a78 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -18,6 +18,7 @@ from numpy import any as array_any # noqa from numpy import ( # noqa around, # noqa + full_like, gradient, isclose, isin, @@ -26,7 +27,6 @@ tensordot, transpose, unravel_index, - zeros_like, # noqa ) from numpy import concatenate as _concatenate from numpy.lib.stride_tricks import sliding_window_view # noqa @@ -151,7 +151,7 @@ def isnull(data): return xp.isnan(data) elif issubclass(scalar_type, (np.bool_, np.integer, np.character, np.void)): # these types cannot represent missing values - return zeros_like(data, dtype=bool) + return full_like(data, dtype=bool, fill_value=False) else: # at this point, array should have dtype=object if isinstance(data, np.ndarray): diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 9167c2ddbea..7757ec58edc 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -577,17 +577,39 @@ def test_argmin_max_error(): @pytest.mark.parametrize( - "array", + ["array", "expected"], [ - np.array([np.datetime64("2000-01-01"), np.datetime64("NaT")]), - np.array([np.timedelta64(1, "h"), np.timedelta64("NaT")]), - np.array([0.0, np.nan]), - np.array([1j, np.nan]), - np.array(["foo", np.nan], dtype=object), + ( + np.array([np.datetime64("2000-01-01"), np.datetime64("NaT")]), + np.array([False, True]), + ), + ( + np.array([np.timedelta64(1, "h"), np.timedelta64("NaT")]), + np.array([False, True]), + ), + ( + np.array([0.0, np.nan]), + np.array([False, True]), + ), + ( + np.array([1j, np.nan]), + np.array([False, True]), + ), + ( + np.array(["foo", np.nan], dtype=object), + np.array([False, True]), + ), + ( + np.array([1, 2], dtype=int), + np.array([False, False]), + ), + ( + np.array([True, False], dtype=bool), + np.array([False, False]), + ), ], ) -def test_isnull(array): - expected = np.array([False, True]) +def test_isnull(array, expected): actual = duck_array_ops.isnull(array) np.testing.assert_equal(expected, actual)