Skip to content

Duck array ops for all and any #9883

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

Merged
merged 9 commits into from
Feb 4, 2025
Next Next commit
Duck array ops for all and any
  • Loading branch information
tomwhite committed Jan 15, 2025
commit 43594b880f3243cf776b8a8c1b7a7071068e2b1d
18 changes: 13 additions & 5 deletions xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import numpy as np
import pandas as pd
from numpy import all as array_all # noqa: F401
from numpy import any as array_any # noqa: F401
from numpy import ( # noqa: F401
isclose,
isnat,
Expand Down Expand Up @@ -319,7 +317,7 @@ def allclose_or_equiv(arr1, arr2, rtol=1e-5, atol=1e-8):
if lazy_equiv is None:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", r"All-NaN (slice|axis) encountered")
return bool(isclose(arr1, arr2, rtol=rtol, atol=atol, equal_nan=True).all())
return bool(array_all(isclose(arr1, arr2, rtol=rtol, atol=atol, equal_nan=True)))
else:
return lazy_equiv

Expand All @@ -333,7 +331,7 @@ def array_equiv(arr1, arr2):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "In the future, 'NAT == x'")
flag_array = (arr1 == arr2) | (isnull(arr1) & isnull(arr2))
return bool(flag_array.all())
return bool(array_all(flag_array))
else:
return lazy_equiv

Expand All @@ -349,7 +347,7 @@ def array_notnull_equiv(arr1, arr2):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "In the future, 'NAT == x'")
flag_array = (arr1 == arr2) | isnull(arr1) | isnull(arr2)
return bool(flag_array.all())
return bool(array_all(flag_array))
else:
return lazy_equiv

Expand Down Expand Up @@ -536,6 +534,16 @@ def f(values, axis=None, skipna=None, **kwargs):
cumsum_1d.numeric_only = True


def array_all(array, axis=None, keepdims=False):
xp = get_array_namespace(array)
return xp.all(array, axis=axis, keepdims=keepdims)


def array_any(array, axis=None, keepdims=False):
xp = get_array_namespace(array)
return xp.any(array, axis=axis, keepdims=keepdims)


_mean = _create_nan_agg_method("mean", invariant_0d=True)


Expand Down
2 changes: 1 addition & 1 deletion xarray/core/weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __init__(self, obj: T_Xarray, weights: T_DataArray) -> None:

def _weight_check(w):
# Ref https://github.com/pydata/xarray/pull/4559/files#r515968670
if duck_array_ops.isnull(w).any():
if duck_array_ops.array_any(duck_array_ops.isnull(w)):
raise ValueError(
"`weights` cannot contain missing values. "
"Missing values can be replaced by `weights.fillna(0)`."
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy