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

Indexing with quantified coordinates #276

Open
SGeeversAtVortech opened this issue Oct 3, 2024 · 1 comment
Open

Indexing with quantified coordinates #276

SGeeversAtVortech opened this issue Oct 3, 2024 · 1 comment

Comments

@SGeeversAtVortech
Copy link

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
Copy link
Collaborator

keewis commented Oct 7, 2024

you have two choices:

  • use pint-xarray's versions of loc / sel: yq.pint.loc[{"x": Q_(1, "s")}] or yq.pint.sel(x=Q_(1, "s"))
  • install pint-xarray from main and try out the new PintIndex. This would mean that your code mostly just works (but I've not been able to test this as much as I would have liked, so any feedback would be appreciated)

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

2 participants