Skip to content

Value based indexing using .sel is broken on datetime coords #4377

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

Closed
philippjfr opened this issue Aug 26, 2020 · 1 comment
Closed

Value based indexing using .sel is broken on datetime coords #4377

philippjfr opened this issue Aug 26, 2020 · 1 comment

Comments

@philippjfr
Copy link

What happened:

Value based indexing using .sel on a datetime coordinate is broken.

Minimal Complete Verifiable Example:

import numpy as np
import pandas
import xarray as xr

a = xr.DataArray(np.arange(27).reshape(3,3,3), 
                 dims=["x", "y", "time"], 
                 coords=dict(x=[1,2,3], y=[1,2,3], time=pd.date_range("2020-01-01", freq="1d", periods=3)))

a.sel(time=a.time.values[0])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-b52cbbe5c7df> in <module>
----> 1 a.sel(time=a.time.values[0])

~/miniconda3/envs/gv_dev/lib/python3.7/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1059             method=method,
   1060             tolerance=tolerance,
-> 1061             **indexers_kwargs,
   1062         )
   1063         return self._from_temp_dataset(ds)

~/miniconda3/envs/gv_dev/lib/python3.7/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2064         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
   2065         pos_indexers, new_indexes = remap_label_indexers(
-> 2066             self, indexers=indexers, method=method, tolerance=tolerance
   2067         )
   2068         result = self.isel(indexers=pos_indexers, drop=drop)

~/miniconda3/envs/gv_dev/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

~/miniconda3/envs/gv_dev/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:

~/miniconda3/envs/gv_dev/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":

~/miniconda3/envs/gv_dev/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: 1577836800000000000

Anything else we need to know?:

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.5 (default, Oct 25 2019, 10:52:18)
[Clang 4.0.1 (tags/RELEASE_401/final)]
python-bits: 64
OS: Darwin
OS-release: 18.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.6.2

xarray: 0.16.0
pandas: 1.1.1
numpy: 1.17.3
scipy: 1.4.1
netCDF4: 1.5.1.2
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: 2.3.2
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.4
cfgrib: None
iris: None
bottleneck: None
dask: 2.9.0
distributed: 2.9.0
matplotlib: 3.1.2
cartopy: 0.17.0
seaborn: 0.10.0
numbagg: None
pint: None
setuptools: 46.1.3.post20200330
pip: 19.3.1
conda: 4.8.3
pytest: 5.3.2
IPython: 7.10.2
sphinx: 2.3.1

@philippjfr
Copy link
Author

Apologies, duplicate of #4283. Closing.

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

No branches or pull requests

1 participant