Skip to content

Commit d34ebff

Browse files
authored
implement isnull using full_like instead of zeros_like (#7395)
* 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`
1 parent 0475435 commit d34ebff

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

xarray/core/duck_array_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from numpy import any as array_any # noqa
1919
from numpy import ( # noqa
2020
around, # noqa
21+
full_like,
2122
gradient,
2223
isclose,
2324
isin,
@@ -26,7 +27,6 @@
2627
tensordot,
2728
transpose,
2829
unravel_index,
29-
zeros_like, # noqa
3030
)
3131
from numpy import concatenate as _concatenate
3232
from numpy.lib.stride_tricks import sliding_window_view # noqa
@@ -151,7 +151,7 @@ def isnull(data):
151151
return xp.isnan(data)
152152
elif issubclass(scalar_type, (np.bool_, np.integer, np.character, np.void)):
153153
# these types cannot represent missing values
154-
return zeros_like(data, dtype=bool)
154+
return full_like(data, dtype=bool, fill_value=False)
155155
else:
156156
# at this point, array should have dtype=object
157157
if isinstance(data, np.ndarray):

xarray/tests/test_duck_array_ops.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,39 @@ def test_argmin_max_error():
577577

578578

579579
@pytest.mark.parametrize(
580-
"array",
580+
["array", "expected"],
581581
[
582-
np.array([np.datetime64("2000-01-01"), np.datetime64("NaT")]),
583-
np.array([np.timedelta64(1, "h"), np.timedelta64("NaT")]),
584-
np.array([0.0, np.nan]),
585-
np.array([1j, np.nan]),
586-
np.array(["foo", np.nan], dtype=object),
582+
(
583+
np.array([np.datetime64("2000-01-01"), np.datetime64("NaT")]),
584+
np.array([False, True]),
585+
),
586+
(
587+
np.array([np.timedelta64(1, "h"), np.timedelta64("NaT")]),
588+
np.array([False, True]),
589+
),
590+
(
591+
np.array([0.0, np.nan]),
592+
np.array([False, True]),
593+
),
594+
(
595+
np.array([1j, np.nan]),
596+
np.array([False, True]),
597+
),
598+
(
599+
np.array(["foo", np.nan], dtype=object),
600+
np.array([False, True]),
601+
),
602+
(
603+
np.array([1, 2], dtype=int),
604+
np.array([False, False]),
605+
),
606+
(
607+
np.array([True, False], dtype=bool),
608+
np.array([False, False]),
609+
),
587610
],
588611
)
589-
def test_isnull(array):
590-
expected = np.array([False, True])
612+
def test_isnull(array, expected):
591613
actual = duck_array_ops.isnull(array)
592614
np.testing.assert_equal(expected, actual)
593615

0 commit comments

Comments
 (0)