From 59df0d8e5db2abca04107c7a6c8c6ed56c328b39 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 20 Dec 2022 16:38:15 +0100 Subject: [PATCH 1/4] use `full_like` to implement `isnull` on non-nullable dtypes --- xarray/core/duck_array_ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index 6aba6617c37..2046c3772d9 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -16,6 +16,7 @@ import pandas as pd from numpy import all as array_all # noqa from numpy import any as array_any # noqa +from numpy import full_like # noqa from numpy import zeros_like # noqa from numpy import around, broadcast_to # noqa from numpy import concatenate as _concatenate @@ -128,7 +129,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): From 4c7590abd303d1f1accf96ea296b50a0006575a3 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 20 Dec 2022 16:40:26 +0100 Subject: [PATCH 2/4] remove the unused import of `zeros_like` --- xarray/core/duck_array_ops.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index 2046c3772d9..8faf430a33d 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -17,7 +17,6 @@ from numpy import all as array_all # noqa from numpy import any as array_any # noqa from numpy import full_like # noqa -from numpy import zeros_like # noqa from numpy import around, broadcast_to # noqa from numpy import concatenate as _concatenate from numpy import ( # noqa From d029b95d7d76d4140f27239f9bded6f2108c2ef8 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Tue, 20 Dec 2022 16:51:56 +0100 Subject: [PATCH 3/4] add tests to make sure non-nullable dtypes still return all-`False` --- xarray/tests/test_duck_array_ops.py | 38 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 2943bd71671..51d55b155e6 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -551,17 +551,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) From d0c67354dbd29267df839273bcc1289138d6abb6 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Wed, 17 Jan 2024 17:59:51 +0100 Subject: [PATCH 4/4] remove the now unused `zeros_like` --- xarray/core/duck_array_ops.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/core/duck_array_ops.py b/xarray/core/duck_array_ops.py index bb091d0b888..0814206a6e5 100644 --- a/xarray/core/duck_array_ops.py +++ b/xarray/core/duck_array_ops.py @@ -27,7 +27,6 @@ tensordot, transpose, unravel_index, - zeros_like, # noqa ) from numpy import concatenate as _concatenate from numpy.core.multiarray import normalize_axis_index # type: ignore[attr-defined]