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

Selection by list of dates as strings failing with KeyError #271

Closed
scottyhq opened this issue Jun 21, 2024 · 0 comments · Fixed by pydata/xarray#9182
Closed

Selection by list of dates as strings failing with KeyError #271

scottyhq opened this issue Jun 21, 2024 · 0 comments · Fixed by pydata/xarray#9182
Labels
bug Something isn't working upstream

Comments

@scottyhq
Copy link
Contributor

scottyhq commented Jun 21, 2024

https://tutorial.xarray.dev/fundamentals/02.1_indexing_Basic.html#indexing-with-a-datetimeindex-or-date-string-list

dates = ['2013-07-09', '2013-10-11', '2013-12-24']
ds.sel(time=dates)
File ~/miniforge3/envs/xarray-tutorial-v1/lib/python3.12/site-packages/xarray/core/dataset.py:3126, in Dataset.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   3058 """Returns a new dataset with each array indexed by tick labels
   3059 along the specified dimension(s).
   3060 
   (...)
   3123 
   3124 """
   3125 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
-> 3126 query_results = map_index_queries(
   3127     self, indexers=indexers, method=method, tolerance=tolerance
   3128 )
   3130 if drop:
   3131     no_scalar_variables = {}

File ~/miniforge3/envs/xarray-tutorial-v1/lib/python3.12/site-packages/xarray/core/indexing.py:192, in map_index_queries(obj, indexers, method, tolerance, **indexers_kwargs)
    190         results.append(IndexSelResult(labels))
    191     else:
--> 192         results.append(index.sel(labels, **options))
    194 merged = merge_sel_results(results)
    196 # drop dimension coordinates found in dimension indexers
    197 # (also drop multi-index if any)
    198 # (.sel() already ensures alignment)

File ~/miniforge3/envs/xarray-tutorial-v1/lib/python3.12/site-packages/xarray/core/indexes.py:801, in PandasIndex.sel(self, labels, method, tolerance)
    799     indexer = get_indexer_nd(self.index, label_array, method, tolerance)
    800     if np.any(indexer < 0):
--> 801         raise KeyError(f"not all values found in index {coord_name!r}")
    803 # attach dimension names and/or coordinates to positional indexer
    804 if isinstance(label, Variable):

KeyError: "not all values found in index 'time'"

Noticed this bumping to latest package versions (with numpy>=2 in environment)

(#270)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant