-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support __array_ufunc__ for xarray objects. #1962
Conversation
This means NumPy ufuncs are now supported directly on xarray.Dataset objects, and opens the door to supporting computation on new data types, such as sparse arrays or arrays with units. Fixes GH1617
xarray/core/common.py
Outdated
dask='allowed') | ||
|
||
|
||
class DataWithCoords(SupportsArithmetic, AttrAccessMixin): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W1641 Implementing eq without also implementing hash
xarray/core/common.py
Outdated
@@ -235,7 +239,65 @@ def get_squeeze_dims(xarray_obj, dim, axis=None): | |||
return dim | |||
|
|||
|
|||
class BaseDataObject(AttrAccessMixin): | |||
class SupportsArithmetic(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W1641 Implementing eq without also implementing hash
xarray/core/common.py
Outdated
@@ -235,7 +239,65 @@ def get_squeeze_dims(xarray_obj, dim, axis=None): | |||
return dim | |||
|
|||
|
|||
class BaseDataObject(AttrAccessMixin): | |||
class SupportsArithmetic(object): # noqa: W1641 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W1641 Implementing eq without also implementing hash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't # noqa: W1641
disable this warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's a pylint warning, not a flake8 warning. It's because py3k
is enabled. We could turn that checker off
https://pylint.readthedocs.io/en/latest/user_guide/message-control.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's what I just did.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks nice.
I could not find any edge cases that break backward compatibility.
One potential edge case is if someone directly calls a ufunc reduce method, e.g., Example:
Note that the more commonly used aliases for these reduce methods, e.g., There are also a few other ufunc methods that get used occasionally. I think I'm OK breaking these because usage is so rare (and the work-around of casting to numpy arrays is so easy) but this should probably be noted in the release notes. |
This looks awesome. Thoughts on where you think units fits in here? |
@dopplershift see #1938. Units should be able to make use of the same machinery as sparse arrays ( |
At this point I'd be happy to have hooks that let me intercept/wrap ufunc operations, though I guess that's what #1938 is supporting in a more systematic way. |
If you try to put a pint array into xarray.DataArray right now, I'm pretty sure it will get cast into a NumPy array. |
Right. But such hooks would be sufficient to properly maintain the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Excited to get this working.
xarray/core/common.py
Outdated
import warnings | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from . import dtypes, formatting, ops | ||
from .pycompat import OrderedDict, basestring, dask_array_type, suppress | ||
from .arithmetic import SupportsArithmetic | ||
from .options import OPTIONS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is OPTIONS
ever used or is it needed in this scope for some reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops we don't need it here.
Since commit c6ea042 (merged in atmtools#143), UnitsAwareDataArray depends on xarray.DataArray.__array_ufunc__ (which in turn depends on numpy 1.13 or newer). This was merged into xarray in pydata/xarray#1962 and added to release 0.10.2. The old UnitsAwareDataArray does not work with xarray>=0.10.2 and the new one does not work with xarray<=0.10.1. Therefore, typhon now depends on xarray>=0.10.2 and numpy>=1.13.
This means NumPy ufuncs are now supported directly on xarray.Dataset objects,
and opens the door to supporting computation on new data types, such as sparse
arrays or arrays with units.
whats-new.rst
for all changes andapi.rst
for new API (remove if this change should not be visible to users, e.g., if it is an internal clean-up, or if this is part of a larger project that will be documented later)