Closed tien-vo closed 1 week ago
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. If you have questions, some answers may be found in our contributing guidelines.
Thanks for the PR, @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` 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 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, do you have any opinions on allowing numpy.ndarray
subclasses? This is very similar to what I tried to do back in #2956.
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
andnumpy
scalars# compare types withis
instead ofisinstance
to allownumpy.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 https://github.com/pydata/xarray/pull/2956.
— Reply to this email directly, view it on GitHub https://github.com/pydata/xarray/pull/9705#issuecomment-2453585514, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJJFVW5N7NMEKE6NQUBMEDZ62JUHAVCNFSM6AAAAABRCI6OOGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJTGU4DKNJRGQ . You are receiving this because you were mentioned.Message ID: @.***>
Instead, what I had envisioned is passing through numpy.ndarray subclasses
+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.