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

Support dictionary-style indexing with .loc #363

Open
lukelbd opened this issue Aug 23, 2022 · 1 comment
Open

Support dictionary-style indexing with .loc #363

lukelbd opened this issue Aug 23, 2022 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@lukelbd
Copy link
Contributor

lukelbd commented Aug 23, 2022

Given the following sample data:

import numpy as np
import xarray as xr
import cf_xarray
lev = np.arange(1, 11)
lev = xr.DataArray(lev, dims='lev', attrs={'units': 'km', 'axis': 'Z'})
data = np.arange(lev.size)
data = xr.DataArray(data, dims='lev', coords={'lev': lev})
print(data.cf.axes)  # prints {'Z': ['lev']}
print(data[{'lev': 0}])  # prints data for level value "1"
print(data.loc[{'lev': 1}])  # prints data for level value "1"

The cf accessor cannot handle dictionary-style position-based indexing:

>>> data.cf[{'Z': 0}]
File ~/miniconda3/lib/python3.10/site-packages/cf_xarray/accessor.py:2402, in CFDataArrayAccessor.__getitem__(self, key)
   2370 """
   2371 Index into a DataArray making use of CF attributes.
   2372
   (...)
   2398 Add additional keys by specifying "custom criteria". See :ref:`custom_criteria` for more.
   2399 """
   2401 if not isinstance(key, str):
-> 2402     raise KeyError(
   2403         f"Cannot use a list of keys with DataArrays. Expected a single string. Received {key!r} instead."
   2404     )
   2406 return _getitem(self, key)

KeyError: "Cannot use a list of keys with DataArrays. Expected a single string. Received {'Z': 0} instead."

It also cannot handle dictionary-style coordinate-based inexing:

>>> data.cf.loc[{'Z': 0}]
File ~/miniconda3/lib/python3.10/site-packages/cf_xarray/accessor.py:576, in _getattr(obj, attr, accessor, key_mappers, wrap_classes, extra_decorator)
    573     func: Callable = attribute
    575 else:
--> 576     raise AttributeError(
    577         f"cf_xarray does not know how to wrap attribute '{type(obj).__name__}.{attr}'. "
    578         "Please file an issue if you have a solution."
    579     )
    581 @functools.wraps(func)
    582 def wrapper(*args, **kwargs):
    583     posargs, arguments = accessor._process_signature(
    584         func, args, kwargs, key_mappers
    585     )

AttributeError: cf_xarray does not know how to wrap attribute 'DataArray.loc'. Please file an issue if you have a solution.

I'd be interested in addressing this but don't currently have time. Thought I'd just get the issue on this project's radar for now (if it wasn't already / I didn't miss an already-open thread).

@dcherian
Copy link
Contributor

👍 should be straightforward. I don't use this syntax, that's why it hasn't been implemented yet :)

@dcherian dcherian added the enhancement New feature or request label Aug 23, 2022
@dcherian dcherian added the help wanted Extra attention is needed label Dec 7, 2022
@dcherian dcherian changed the title Feature request: Support dictionary-style indexing Support dictionary-style indexing with .loc Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants