Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xarray.DataArray is not part of pint.Unit's upcast types? #2126

Open
doronbehar opened this issue Feb 4, 2025 · 0 comments
Open

xarray.DataArray is not part of pint.Unit's upcast types? #2126

doronbehar opened this issue Feb 4, 2025 · 0 comments

Comments

@doronbehar
Copy link
Contributor

So taking first steps with pint-xarray, I tried to do this:

import numpy as np
import pint_xarray
import xarray as xr
import pint
ureg = pint.UnitRegistry()

da = xr.DataArray(
    ureg.Quantity(
        np.array([2,3]),
        ureg.meter,
    ),
    coords={'person': ["a", "b"]},
)
ureg.kilogram/ da

And was surprised to find out it fails with:

Traceback (most recent call last):
  File "/home/doron/repos/lab-ion-trap-simulations/./t.py", line 17, in <module>
    ureg.kilogram / da
    ~~~~~~~~~~~~~~^~~~
  File "/nix/store/bb54xf04qx1j3hskjcyjwzhbnkhi666m-python3-3.12.8-env/lib/python3.12/site-packages/pint/facets/plain/unit.py", line 167, in __truediv__
    return self._REGISTRY.Quantity(1 / other, self._units)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/bb54xf04qx1j3hskjcyjwzhbnkhi666m-python3-3.12.8-env/lib/python3.12/site-packages/pint/facets/plain/quantity.py", line 190, in __new__
    raise TypeError(f"PlainQuantity cannot wrap upcast type {type(value)}")
TypeError: PlainQuantity cannot wrap upcast type <class 'xarray.core.dataarray.DataArray'>

Whereas:

da**-1 * ureg.kilogram
ureg.kilogram * 1/da
1*ureg.kilogram / da
ureg.kilogram * da # Different meaning of course

Don't fail. This issue was originally posted at xarray-contrib/pint-xarray#289 but I got convinced that the fix for it should be done here and not there, as pint explicitly puts xarray.DataArray (and probably xarray.Dataset as well) in the "upcast types" list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant