Skip to content

String handling when using .sel (method='nearest') to find a location #10276

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

Open
Breeze-Hu opened this issue Apr 30, 2025 · 1 comment
Open

Comments

@Breeze-Hu
Copy link

Is your feature request related to a problem?

Hi!

Below I have provided a scenario that may not be code that minimally reproduces the problem, but had to be done to more clearly describe the meaning of the behaviour.

import xarray as xr
import numpy as np
import pandas as pd
 
# example data
time = pd.date_range("2000-01-01", "2001-12-31", freq="D")
time = time[~((time.month == 2) & (time.day == 29))] 

lon = np.linspace(100, 110, 5)
lat = np.linspace(30, 35, 4)
data = np.random.rand(len(time), len(lon), len(lat))

da = xr.DataArray(
    data,
    coords={"time": time, "lon": lon, "lat": lat},
    dims=["time", "lon", "lat"],
    name="pr"
)

# Split based on year and month-days
da1 = da.assign_coords(year = da.time.dt.year, monthday = da.time.dt.strftime("%m-%d")).groupby(['year', 'monthday']).first()

# Make a selection on the data of da1:
da1.sel(year = 2000, monthday = '03-24', lon = 105, lat = 30) # It's ok

# Problems arose once the method was used for the most recently search:
da1.sel(year = 2000, monthday = '03-24', lon = 105, lat = 30, method='nearest') 

Describe the solution you'd like

I'm guessing the problem is related to its type: monthday (monthday) object

# I know there are many solutions. For example:
da1.sel(year = 2000, monthday = '03-24').sel(lon = 105, lat = 30, method='nearest')

For greater clarity and simplicity in the use of indexes, is it possible to add judgement to the function?

  • Firstly make a judgement on the type of the sel, it is this type that can't be used with method = “nearest”, such as strings, etc., and if it doesn't match exactly break the operation and point it out in the error message.
  • After the types that can't be “nearest” have been processed, then the subsequent selections are used.

Describe alternatives you've considered

No response

Additional context

No response

@Breeze-Hu Breeze-Hu changed the title String handling when using .sel (method='nearest) to find a location String handling when using .sel (method='nearest') to find a location Apr 30, 2025
@benbovy
Copy link
Member

benbovy commented May 7, 2025

Another solution (pretty clean IMO) to this kind of issue would be to allow passing selection options along with groups of one or more coordinate indexers, as suggested in #7099.

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

No branches or pull requests

2 participants