pint-xarray icon indicating copy to clipboard operation
pint-xarray copied to clipboard

Can .pint.magnitude return a DataArray instead?

Open dcherian opened this issue 3 years ago • 8 comments

Currently DataArray.pint.magnitude returns a numpy array. Can it return a DataArray instead? We can always extract the numpy array using DataArray.data.magnitude.

Just realized I can also use DataArray.as_numpy() but perhaps this is still worth considering?

dcherian avatar Jan 21 '22 23:01 dcherian

I would be okay with this - what say you @keewis?

TomNicholas avatar Jan 22 '22 00:01 TomNicholas

this feels pretty similar to .pint.dequantify().drop_attrs("units") (if we had something like .drop_attrs), but it's true that .data.magnitude is much more accessible than that. So I guess defining .pint.magnitude to return a DataArray does make sense?

keewis avatar Jan 31 '22 23:01 keewis

If we are going to make this change then we should 100% do it before I release that belated blog post. I would be happy to implement this if we all agree?

TomNicholas avatar Feb 01 '22 17:02 TomNicholas

:+1: The one place as_numpy doesn't work is when it's DataArray(pint(dask(numpy)))

dcherian avatar Feb 01 '22 17:02 dcherian

The one place as_numpy doesn't work is when it's DataArray(pint(dask(numpy)))

"Doesn't work" as in .pint.magnitude returning a DataArray would be the only way to conveniently get back DataArray(dask(numpy))?

TomNicholas avatar Feb 01 '22 18:02 TomNicholas

^ Yes AFAIK

dcherian avatar Feb 01 '22 18:02 dcherian

I do agree to make the change, but I would argue that .pint.dequantify() should be used to convert between DataArray(pint(dask(numpy))) and DataArray(dask(numpy)) (and if that doesn't work we should fix it). For that, we might want to have the option to save the Unit object instead of the formatted string.

keewis avatar Feb 01 '22 20:02 keewis

coming back to this, I think that .pint.dequantify() does what you are looking for, but we might want to allow selectively dequantifying just a subset of the variables instead.

Edit: I'll leave this open until I've come to a conclusion regarding selectively dequantifying variables

keewis avatar Jun 23 '24 13:06 keewis