Skip to content

Commit

Permalink
implement isnull using full_like instead of zeros_like (#7395)
Browse files Browse the repository at this point in the history
* use `full_like` to implement `isnull` on non-nullable dtypes

* remove the unused import of `zeros_like`

* add tests to make sure non-nullable dtypes still return all-`False`

* remove the now unused `zeros_like`
  • Loading branch information
keewis committed Jan 23, 2024
1 parent 0475435 commit d34ebff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from numpy import any as array_any # noqa
from numpy import ( # noqa
around, # noqa
full_like,
gradient,
isclose,
isin,
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
38 changes: 30 additions & 8 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit d34ebff

Please sign in to comment.