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

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

Open
seth-p opened this issue Aug 21, 2020 · 2 comments

Comments

@seth-p
Copy link
Contributor

seth-p commented Aug 21, 2020

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

@seth-p seth-p changed the title Indexing a datetime64[ns] coordinate with a datetime.date object produces a KeyError Indexing a datetime64[ns] coordinate with a scalar datetime.date object produces a KeyError Aug 22, 2020
@seth-p seth-p changed the title Indexing a datetime64[ns] coordinate with a scalar datetime.date object produces a KeyError Indexing a datetime64[ns] coordinate with a scalar datetime.date produces a KeyError Aug 26, 2020
@shoyer
Copy link
Member

shoyer commented Sep 16, 2020

This doesn't work in pandas either:

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

~/miniconda3/envs/xarray-py38/lib/python3.8/site-packages/pandas/core/indexing.py in __getitem__(self, key)
    877
    878             maybe_callable = com.apply_if_callable(key, self.obj)
--> 879             return self._getitem_axis(maybe_callable, axis=axis)
    880
    881     def _is_scalar_access(self, key: Tuple):

~/miniconda3/envs/xarray-py38/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1108         # fall thru to straight lookup
   1109         self._validate_key(key, axis)
-> 1110         return self._get_label(key, axis=axis)
   1111
   1112     def _get_slice_axis(self, slice_obj: slice, axis: int):

~/miniconda3/envs/xarray-py38/lib/python3.8/site-packages/pandas/core/indexing.py in _get_label(self, label, axis)
   1057     def _get_label(self, label, axis: int):
   1058         # GH#5667 this will fail if the label is not present in the axis.
-> 1059         return self.obj.xs(label, axis=axis)
   1060
   1061     def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):

~/miniconda3/envs/xarray-py38/lib/python3.8/site-packages/pandas/core/generic.py in xs(self, key, axis, level, drop_level)
   3480             loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
   3481         else:
-> 3482             loc = self.index.get_loc(key)
   3483
   3484             if isinstance(loc, np.ndarray):

~/miniconda3/envs/xarray-py38/lib/python3.8/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)

I think it is reasonable to expect a date object to work like a date string, but I would suggest fixing this in pandas first (inside DatetimeIndex.get_loc), at which point it should automatically work for xarray.

@seth-p
Copy link
Contributor Author

seth-p commented Sep 19, 2020

Indeed, this is reported in pandas-dev/pandas#35466 (comment) and pandas-dev/pandas#35830. Also pandas-dev/pandas#35478.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants