Skip to content

Commit

Permalink
Replace more usages of np.any with duck array ops equivalent
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Jan 15, 2025
1 parent fda6a5b commit d8367b2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from xarray.core import indexing
from xarray.core.common import contains_cftime_datetimes, is_np_datetime_like
from xarray.core.duck_array_ops import array_all, asarray, ravel, reshape
from xarray.core.duck_array_ops import array_all, array_any, asarray, ravel, reshape
from xarray.core.formatting import first_n_items, format_timestamp, last_item
from xarray.core.pdcompat import nanosecond_precision_timestamp, timestamp_as_unit
from xarray.core.utils import attempt_import, emit_user_level_warning
Expand Down Expand Up @@ -824,7 +824,7 @@ def _cast_to_dtype_if_safe(num: np.ndarray, dtype: np.dtype) -> np.ndarray:
"a larger integer dtype."
)
else:
if np.isinf(cast_num).any():
if array_any(np.isinf(cast_num)):
raise OverflowError(
f"Not possible to cast encoded times from {num.dtype!r} to "
f"{dtype!r} without overflow. Consider removing the dtype "
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pandas.errors import OutOfBoundsDatetime

from xarray.core.datatree_render import RenderDataTree
from xarray.core.duck_array_ops import array_all, array_equiv, astype
from xarray.core.duck_array_ops import array_all, array_any, array_equiv, astype
from xarray.core.indexing import MemoryCachedArray
from xarray.core.options import OPTIONS, _get_boolean_with_default
from xarray.core.treenode import group_subtrees
Expand Down Expand Up @@ -232,7 +232,7 @@ def format_array_flat(array, max_width: int):

cum_len = np.cumsum([len(s) + 1 for s in relevant_items]) - 1
if (array.size > 2) and (
(max_possibly_relevant < array.size) or (cum_len > max_width).any()
(max_possibly_relevant < array.size) or array_any(cum_len > max_width)
):
padding = " ... "
max_len = max(int(np.argmax(cum_len + len(padding) - 1 > max_width)), 2)
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _nan_argminmax_object(func, fill_value, value, axis=None, **kwargs):
data = getattr(np, func)(value, axis=axis, **kwargs)

# TODO This will evaluate dask arrays and might be costly.
if (valid_count == 0).any():
if duck_array_ops.array_any(valid_count == 0):
raise ValueError("All-NaN slice encountered")

return data
Expand Down

0 comments on commit d8367b2

Please sign in to comment.