xarray-contrib / xarray-contrib/pint-xarray

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

Open
#303 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
119
Forks
15
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal example with Python, xarray, and pint-xarray, then inspect the PintIndex.sel entry point reported in the traceback. The issue is done when q.sel(lat=74 * ureg.degrees, method="nearest") succeeds without the unexpected-keyword error and selects the nearest coordinate.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.