Skip to content

Indexing a datetime64[ns] coordinate with a scalar datetime.date produces a KeyError #4363

@seth-p

Description

@seth-p

Indexing a datetime64[ns] coordinate with a scalar datetime.date produces a KeyError ([6]).
Curiously, indexing with a datetime.date slice does work ([5]).
I would expect [6] to work just like [4].

This may well be related to (or a duplicate of) #3736, #4283, #4292, #4306, #4319, or #4370, but none of those actually mentions datetime.date objects, so I can't tell.

In [1]: import xarray as xr, pandas as pd, datetime as dt

In [2]: x = xr.DataArray([1., 2., 3.], [('foo', pd.date_range('2010-01-01', periods=3))])

In [3]: x
Out[3]: 
<xarray.DataArray (foo: 3)>
array([1., 2., 3.])
Coordinates:
  * foo      (foo) datetime64[ns] 2010-01-01 2010-01-02 2010-01-03

In [4]: x.loc[dt.datetime(2010, 1, 1)]
Out[4]: 
<xarray.DataArray ()>
array(1.)
Coordinates:
    foo      datetime64[ns] 2010-01-01

In [5]: x.loc[dt.date(2010, 1, 1):dt.date(2010, 1, 3)]
Out[5]: 
<xarray.DataArray (foo: 3)>
array([1., 2., 3.])
Coordinates:
  * foo      (foo) datetime64[ns] 2010-01-01 2010-01-02 2010-01-03

In [6]: x.loc[dt.date(2010, 1, 1)]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-8ef314626f7d> in <module>
----> 1 x.loc[dt.date(2010, 1, 1)]

~/.conda/envs/build/lib/python3.7/site-packages/xarray/core/dataarray.py in __getitem__(self, key)
    196             labels = indexing.expanded_indexer(key, self.data_array.ndim)
    197             key = dict(zip(self.data_array.dims, labels))
--> 198         return self.data_array.sel(**key)
    199 
    200     def __setitem__(self, key, value) -> None:

~/.conda/envs/build/lib/python3.7/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1152             method=method,
   1153             tolerance=tolerance,
-> 1154             **indexers_kwargs,
   1155         )
   1156         return self._from_temp_dataset(ds)

~/.conda/envs/build/lib/python3.7/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2100         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
   2101         pos_indexers, new_indexes = remap_label_indexers(
-> 2102             self, indexers=indexers, method=method, tolerance=tolerance
   2103         )
   2104         result = self.isel(indexers=pos_indexers, drop=drop)

~/.conda/envs/build/lib/python3.7/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    395 
    396     pos_indexers, new_indexes = indexing.remap_label_indexers(
--> 397         obj, v_indexers, method=method, tolerance=tolerance
    398     )
    399     # attach indexer's coordinate to pos_indexers

~/.conda/envs/build/lib/python3.7/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    268             coords_dtype = data_obj.coords[dim].dtype
    269             label = maybe_cast_to_coords_dtype(label, coords_dtype)
--> 270             idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance)
    271             pos_indexers[dim] = idxr
    272             if new_idx is not None:

~/.conda/envs/build/lib/python3.7/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
    188             else:
    189                 indexer = index.get_loc(
--> 190                     label.item(), method=method, tolerance=tolerance
    191                 )
    192         elif label.dtype.kind == "b":

~/.conda/envs/build/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
    620         else:
    621             # unrecognized type
--> 622             raise KeyError(key)
    623 
    624         try:

KeyError: datetime.date(2010, 1, 1)

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:25:08)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-693.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.6
libnetcdf: 4.7.4

xarray: 0.16.0
pandas: 1.1.0
numpy: 1.19.1
scipy: 1.5.2
netCDF4: 1.5.4
pydap: None
h5netcdf: 0.8.1
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.23.0
distributed: 2.23.0
matplotlib: 3.3.1
cartopy: None
seaborn: 0.10.1
numbagg: installed
pint: None
setuptools: 49.6.0.post20200814
pip: 20.2.2
conda: 4.8.4
pytest: None
IPython: 7.17.0
sphinx: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions