-
-
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
Allow wrapping astropy.units.Quantity #9705
Conversation
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Thanks for the PR, @tien-vo. This is good a start, but I believe we should try to avoid special-casing if isinstance(data, np.matrix):
data = np.asarray(data)
# immediately return array-like types except `numpy.ndarray` and `numpy` scalars
# compare types with `is` instead of `isinstance` to allow `numpy.ndarray` subclasses
is_numpy = type(data) is np.ndarray or isinstance(data, np.generic)
if not is_numpy and (hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")):
return cast("T_DuckArray", data) and any tests can be added to @shoyer, do you have any opinions on allowing |
Allowing ndarray subclasses that are not matrix or MaskedArray sounds good
to me!
…On Sun, Nov 3, 2024 at 1:38 PM Justus Magin ***@***.***> wrote:
Thanks for the PR, @tien-vo <https://github.com/tien-vo>.
This is good a start, but I believe we should try to avoid special-casing
astropy.units.Quantity as much as possible. Instead, what I had
envisioned is passing through numpy.ndarray subclasses (except np.matrix
and np.ma.MaskedArray), but not numpy.ndarray itself. The condition would
then be something like
if isinstance(data, np.matrix):
data = np.asarray(data)
# immediately return array-like types except `numpy.ndarray` and `numpy` scalars# compare types with `is` instead of `isinstance` to allow `numpy.ndarray` subclassesis_numpy = type(data) is np.ndarray or isinstance(data, np.generic)if not is_numpy and (hasattr(data, "__array_function__") or hasattr(data, "__array_namespace__")):
return cast("T_DuckArray", data)
and any tests can be added to
xarray/tests/test_variable.py::TestAsCompatibleData. You could call it
test_numpy_subclass, and similarly to test_unsupported_type it may be
enough to create a very simple numpy.ndarray subclass and pass that to
the constructor of xr.Variable.
@shoyer <https://github.com/shoyer>, do you have any opinions on allowing
numpy.ndarray subclasses? This is very similar to what I tried to do back
in #2956 <#2956>.
—
Reply to this email directly, view it on GitHub
<#9705 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJJFVW5N7NMEKE6NQUBMEDZ62JUHAVCNFSM6AAAAABRCI6OOGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJTGU4DKNJRGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
+1 to this! Was just playing around with a very simple numpy subclass and was surprised to see that this is not currently supported. |
I started #9760 just because I would love to get this feature added soon. |
Ticks off first item in #9704 and also astropy #14454 .
The conditions added catch
astropy.units.Quantity
type while banningnumpy.matrix
andnumpy.ma.masked_array
, as suggested in #525 , i.e.,@keewis The above can be put into tests. I'm just not sure where. xarray/tests/test_units.py seems busy with
pint
-related stuff.