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

Issue with `.sel` and `PintIndex` when using kwarg `method="nearest"`

Open bpatel2107 opened this issue 6 months ago • 3 comments

I have tried using the latest version of pint-xarray==0.5.0 which includes units in the coordinates which is exciting but I believe I have found an issue when using .sel and the kwarg method="nearest", which would be used when selecting data nearest to the coordinate value specified but not exactly on the grid.

My guess is that the kwargs are not being passed correctly. Maintaining this functionality would be incredibly useful and hopefully an easy fix.

See a minimal working example below

import xarray as xr
import pint_xarray

ureg = pint_xarray.unit_registry
ds = xr.tutorial.open_dataset("air_temperature")
q = ds.pint.quantify({"lat": "degrees", "lon": "degrees"})

q_75 = q.sel(lat=75 * ureg.degrees)
q_74 = q.sel(lat=74 * ureg.degrees, method="nearest")

which fails with this error

Traceback (most recent call last):
  File "/home/ir-pate3/pyro_testing/mwe_pint_xarray.py", line 10, in <module>
    q_74 = q.sel(lat=74 * ureg.degrees, method="nearest")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ir-pate3/.local/lib/python3.11/site-packages/xarray/core/dataset.py", line 3249, in sel
    query_results = map_index_queries(
                    ^^^^^^^^^^^^^^^^^^
  File "/home/ir-pate3/.local/lib/python3.11/site-packages/xarray/core/indexing.py", line 194, in map_index_queries
    results.append(index.sel(labels, **options))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: PintIndex.sel() got an unexpected keyword argument 'method'

Currently using pint-xarray=0.5.0 and xarray=2024.11.0.

See related issue here where this breaks our CI tests. https://github.com/pyro-kinetics/pyrokinetics/issues/446

bpatel2107 avatar Jun 10 '25 11:06 bpatel2107