Skip to content
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

implement isnull using full_like instead of zeros_like #7395

Merged
merged 6 commits into from
Jan 23, 2024
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 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.core.multiarray import normalize_axis_index # type: ignore[attr-defined]
Expand Down Expand Up @@ -141,7 +141,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
Loading