Skip to content

Improved duck array wrapping #9798

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 30 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fd6b339
lots more duck array compat, plus tests
slevang Nov 18, 2024
893408c
Merge branch 'main' into more-array-api-compat
slevang Nov 18, 2024
f7866ce
merge sliding_window_view
slevang Nov 18, 2024
90037fe
namespaces constant
slevang Nov 18, 2024
5ba1a2f
revert dask allowed
slevang Nov 18, 2024
6225ae3
fix up some tests
slevang Nov 19, 2024
e2911c2
backwards compat sparse mask
slevang Nov 19, 2024
2ac37f9
add as_array methods
slevang Nov 21, 2024
1cc344b
to_like_array helper
slevang Nov 21, 2024
69080a5
Merge branch 'main' into more-array-api-compat
slevang Nov 21, 2024
372439c
only cast non-numpy
slevang Nov 21, 2024
0eef2cb
better idxminmax approach
slevang Nov 21, 2024
6739504
fix mypy
slevang Nov 21, 2024
9e6d6f8
naming, add is_array_type
slevang Nov 21, 2024
e721011
add public doc and whats new
slevang Nov 21, 2024
1fe4131
update comments
slevang Nov 21, 2024
205c199
add support for chunked arrays in as_array_type
slevang Nov 21, 2024
7752088
Merge branch 'main' into more-array-api-compat
slevang Nov 21, 2024
c8d4e5e
revert array_type methods
slevang Nov 22, 2024
e67a819
Merge branch 'main' into more-array-api-compat
slevang Nov 22, 2024
f306768
fix up whats new
slevang Nov 22, 2024
18ebdcd
comment about bool_
slevang Nov 22, 2024
f51e3fb
Merge branch 'main' into more-array-api-compat
slevang Nov 22, 2024
121af9e
add jax to complete ci envs
slevang Nov 23, 2024
472ae7e
add pint and sparse to tests
slevang Nov 23, 2024
5aa4a39
remove from windows
slevang Nov 23, 2024
390df6f
mypy, xfail one more sparse
slevang Nov 23, 2024
f6074d2
add dask and a few other methods
slevang Nov 25, 2024
561f21b
Merge branch 'main' into more-array-api-compat
slevang Nov 25, 2024
bfd6aeb
move whats new
slevang Nov 25, 2024
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
Prev Previous commit
Next Next commit
fix up some tests
  • Loading branch information
slevang committed Nov 19, 2024
commit 6225ae3a70d785047259decda130513d1e5df03a
9 changes: 6 additions & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8679,9 +8679,12 @@ def _integrate_one(self, coord, datetime_unit=None, cumulative=False):
else:
if k in self.data_vars and dim in v.dims:
# cast coord data to duck array if needed
coord_data = duck_array_ops.get_array_namespace(v.data).asarray(
coord_var.data
)
if isinstance(v.data, array_type("cupy")):
coord_data = duck_array_ops.get_array_namespace(v.data).asarray(
coord_var.data
)
else:
coord_data = coord_var.data
if _contains_datetime_like_objects(v):
v = datetime_to_numeric(v, datetime_unit=datetime_unit)
if cumulative:
Expand Down
3 changes: 2 additions & 1 deletion xarray/core/duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ def isnull(data):
)
):
# these types cannot represent missing values
return full_like(data, dtype=xp.bool, fill_value=False)
dtype = xp.bool_ if hasattr(xp, "bool_") else xp.bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe xp.bool_ is only needed for compatibility with NumPy 1.x releases. Can we make that clearer here, e.g., by using np.bool_ and adding a comment about NumPy versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, but also cupy only has bool_ at the moment. But If I passed built-in bool to this I think I got a cupy error. It was a weird range from like numpy 1.2x until 2 that they deprecated bool and only had bool_, then brought it back in 2.0 🤷

return full_like(data, dtype=dtype, fill_value=False)
else:
# at this point, array should have dtype=object
if isinstance(data, np.ndarray) or is_extension_array_dtype(data):
Expand Down
3 changes: 3 additions & 0 deletions xarray/core/nputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def f(values, axis=None, **kwargs):
# bottleneck does not take care dtype, min_count
kwargs.pop("dtype", None)
result = bn_func(values, axis=axis, **kwargs)
# bottleneck returns python scalars for reduction over all axes
if isinstance(result, float):
result = np.float64(result)
else:
result = getattr(npmodule, name)(values, axis=axis, **kwargs)

Expand Down
6 changes: 3 additions & 3 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,17 +1936,17 @@ def quantile(
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)

xp = duck_array_ops.get_array_namespace(self.data)

scalar = utils.is_scalar(q)
q = xp.atleast_1d(xp.asarray(q, dtype=float))
q = np.atleast_1d(np.asarray(q, dtype=np.float64))

if dim is None:
dim = self.dims

if utils.is_scalar(dim):
dim = [dim]

xp = duck_array_ops.get_array_namespace(self.data)

def _wrapper(npa, **kwargs):
# move quantile axis to end. required for apply_ufunc
return xp.moveaxis(_quantile_func(npa, **kwargs), 0, -1)
Expand Down
Loading
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