xarray-contrib / xarray-tutorial

Xarray Tutorials
https://tutorial.xarray.dev/
Apache License 2.0
166 stars 106 forks source link

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

Closed scottyhq closed 1 week ago

scottyhq commented 2 weeks ago

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)

(https://github.com/xarray-contrib/xarray-tutorial/pull/270)