xarray-contrib / pint-xarray

Interface for using pint with xarray, providing convenience accessors
https://pint-xarray.readthedocs.io/en/latest/
Apache License 2.0
105 stars 12 forks source link

Indexing with quantified coordinates #276

Open SGeeversAtVortech opened 1 month ago

SGeeversAtVortech commented 1 month ago

It seems not possible to index an array with quantified coordinates; see the example below.

import pint
import pint_xarray
import xarray as xr

unit_registry = pint.UnitRegistry(force_ndarray_like=True)
Q_ = unit_registry.Quantity

y = xr.DataArray(name="y", data=[1, 2], coords={"x": [1, 2]})
yq = y.pint.quantify({"y": "km", "x": "s"})
yq.loc[Q_(1, "s")]  # UnitStrippedWarning
yq.loc[Q_(1000, "ms")]  # KeyError

The line yq.loc[Q_(1, "s")] will raise a UnitStrippedWarning and the line yq.loc[Q_(1000, "ms")] will give the same warning and then raise a KeyError.

Should one just avoid using quantified coordinates? Or are there plans to enable indexing with quantities?

keewis commented 1 month ago

you have two choices: