-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Open
Labels
Description
What is the problem this feature will solve?
I would like to be able to store astropy Quantitys in xarray DataSets. Trying to put a Quantity inside a DataArray doesn't error, but it is not conserved.
import astropy.units as u
import numpy as np
import xarray as xr
array = np.linspace(0, 5, 10) * u.s
data_array = xr.DataArray(data=array, dims="x")
print(f'{data_array=}')
print(f'{data_array.data=}')
print(f'{type(data_array.data)=}')data_array=<xarray.DataArray (x: 10)>
array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
Dimensions without coordinates: x
data_array.data=array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
type(data_array.data)=<class 'numpy.ndarray'>Describe the desired outcome
The minimal goal is to have data_array.data return a Quantity, but it currently returns a bare array. The ideal goal would be to support arithmetic operations on DataArray with a Quantity that pass through units.
I appreciate this is likely not easy, and may require changes to astropy and/or xarray, but I wanted to open this issue to provide a place to log progress, and also register interest in this new feature.
Additional context
- Relevant past astropy issue: astropy - xarray + dask array #12600
- Upstream
xarrayissue: support for units pydata/xarray#525 xarrayblog post on unit-aware arithmetic with pint (another units library): https://xarray.dev/blog/introducing-pint-xarrayxarraydocs on integrating with duck arrays: https://docs.xarray.dev/en/stable/internals/duck-arrays-integration.html- The tests currently in
xarrayfor unit support with Pint: https://github.com/pydata/xarray/blob/185e28e08552b9541844a6cfc5c8875c14c1df28/xarray/tests/test_units.py