1

I am trying to index DataArray of a variable using the "Time" coordinate. It looks something like that:

for time in var.coords["Time"]:
    data = var.sel(Time=time.data)

Using this syntax it fails with:

 File "/user/miniconda3/envs/wrf/lib/python3.7/site-packages/xarray/core/dataarray.py", line 1154, in sel
    **indexers_kwargs,
  File "/user/miniconda3/envs/wrf/lib/python3.7/site-packages/xarray/core/dataset.py", line 2102, in sel
    self, indexers=indexers, method=method, tolerance=tolerance
  File "/user/miniconda3/envs/wrf/lib/python3.7/site-packages/xarray/core/coordinates.py", line 397, in remap_label_indexers
    obj, v_indexers, method=method, tolerance=tolerance
  File "/user/miniconda3/envs/wrf/lib/python3.7/site-packages/xarray/core/indexing.py", line 270, in remap_label_indexers
    idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance)
  File "/user/miniconda3/envs/wrf/lib/python3.7/site-packages/xarray/core/indexing.py", line 190, in convert_label_indexer
    label.item(), method=method, tolerance=tolerance
  File "/user/miniconda3/envs/wrf/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py", line 622, in get_loc
    raise KeyError(key)
KeyError: 1588161600000000000

I thought it should be possible to index using datetime64. Using the string as in the docs works

data = var.sel(Time=str(time.data))

Can somebody explain why it is not possible to index with the raw datetime64 object ?

Thank you in advance

  • You should check the dtype of your time variable. If it's 'object', try treating your time variable as in [this answer](https://stackoverflow.com/a/62604281/3657988) and see if that helps. – Dan Aug 03 '20 at 20:40

0 Answers0