Skip to content

Support astropy Quantities in xarray DataSets #14454

@dstansby

Description

@dstansby

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions