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

Add support for nump types in sel() #470

Open
sandorkertesz opened this issue Sep 20, 2024 · 1 comment
Open

Add support for nump types in sel() #470

sandorkertesz opened this issue Sep 20, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@sandorkertesz
Copy link
Collaborator

Is your feature request related to a problem? Please describe.

At the moment sel() does not accept numpy types. So while this works:

ds.sel(param="t")

this call gives empty result:

ds.sel(param=np._str("t"))

Describe the solution you'd like

Allow using numpy types in sel.

Describe alternatives you've considered

No response

Additional context

No response

Organisation

ECMWF

@sandorkertesz sandorkertesz added the enhancement New feature or request label Sep 20, 2024
@sandorkertesz sandorkertesz self-assigned this Sep 20, 2024
alioacar added a commit to alioacar/earthkit-data that referenced this issue Oct 3, 2024
- Added support for NumPy scalars by checking if the item has an 'item' method and calling it when applicable.
- Converted non-scalar NumPy arrays to lists to ensure consistent handling in the selection process.
alioacar added a commit to alioacar/earthkit-data that referenced this issue Oct 3, 2024
@alioacar
Copy link

alioacar commented Oct 3, 2024

Hi, I believe the issue lies within the Selection class in index.py. The item and shape methods can be utilized to determine whether the variable is a np scalar or an array.

if hasattr(v, 'item') and callable(v.item):
    if not hasattr(v, 'shape') or not v.shape:
        v = v.item()
    else:
        v = v.tolist()

If it is a np array, we check each element to support scenarios where the array may contain mixed types. (Not sure if this is the best way)

v = [item.item() if hasattr(item, 'item') and
      callable(item.item) and (not hasattr(item, 'shape')
      or not item.shape) else item for item in v]

This logic accommodates all the following combinations

np.str_('t')
't'
['t','2t']
np.array((np.str('2t'),'t'))

Additionally, we need to pass the assertion for np arrays in normalize_selection from select.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants