xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Astropy quantities are converted to bare arrays when creating an Index

Open hafarooki opened this issue 2 months ago • 3 comments

What is your issue?

When provided as a data variable, an astropy Quantity is kept as a Quantity. But when provided as an index through the coords argument when constructing a Dataset, the Quantity is converted to an array, erasing the units. Is it possible to fix this?

hafarooki avatar Nov 13 '25 21:11 hafarooki

that's to be expected, see the list of operations converting to numpy. You can work around this using a custom index, but this is going to be quite a bit of effort (you can use pint-xarray's index as an example).

keewis avatar Nov 14 '25 10:11 keewis

I wonder if a "basic array index", as prototyped by @benbovy here, would be a good default for this case.

Basically we'd use PandasIndex only if the provided data are numpy (or basic python types that we cast to numpy), and this "array index" is used otherwise to preserve any attached metadata.

dcherian avatar Nov 14 '25 14:11 dcherian

Yes I think a basic array index would help for such case. Even better here would be an Astropy-specific custom index (see https://github.com/pydata/xarray/pull/9543#issuecomment-2385562215 and https://xarray-indexes.readthedocs.io/blocks/transform.html)!

benbovy avatar Nov 17 '25 10:11 benbovy