Skip to content

Selection with datetime64[ns] fails with Pandas 1.1.0 #4283

Closed
@dopplershift

Description

@dopplershift

I ran into this issue with a netCDF file with the following time variable:

	double time1(time1) ;
		time1:_FillValue = NaN ;
		time1:standard_name = "time" ;
		time1:long_name = "time" ;
		time1:udunits = "Hour since 2017-09-05T12:00:00Z" ;
		time1:units = "Hour since 2017-09-05T12:00:00+00:00" ;
		time1:calendar = "proleptic_gregorian" ;

 time1 = 0, 3, 6, 9, 12, 15, 18, 21, 24 ;

but we can reproduce the problem with something as simple as:

import numpy as np
import xarray as xr

t = np.array(['2017-09-05T12:00:00.000000000', '2017-09-05T15:00:00.000000000'], dtype='datetime64[ns]')
da = xr.DataArray(np.ones(t.shape), dims=('time',), coords=(t,))

da.loc[{'time':t[0]}]  # Works on pandas 1.0.5

this produces:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-3e0afa0bd195> in <module>
----> 1 da.loc[{'time':t[0]}]

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

~/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1147 
   1148         """
-> 1149         ds = self._to_temp_dataset().sel(
   1150             indexers=indexers,
   1151             drop=drop,

~/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2099         """
   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         )

~/miniconda3/envs/py38/lib/python3.8/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    394     }
    395 
--> 396     pos_indexers, new_indexes = indexing.remap_label_indexers(
    397         obj, v_indexers, method=method, tolerance=tolerance
    398     )

~/miniconda3/envs/py38/lib/python3.8/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/py38/lib/python3.8/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
    187                 indexer = index.get_loc(label.item())
    188             else:
--> 189                 indexer = index.get_loc(
    190                     label.item(), method=method, tolerance=tolerance
    191                 )

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

what's interesting is changing the units of datetime64 to [s] works:

import numpy as np
import xarray as xr

t = np.array(['2017-09-05T12:00:00.000000000', '2017-09-05T15:00:00.000000000'], dtype='datetime64[s]')
da = xr.DataArray(np.ones(t.shape), dims=('time',), coords=(t,))
da.loc[{'time':t[0]}]  # Works

Environment:
Python 3.8 from conda-forge on macOS 10.15.4

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.5 | packaged by conda-forge | (default, Jul 24 2020, 01:06:20)
[Clang 10.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 19.6.0
machine: x86_64
processor: i386
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: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: 0.9.8.3
iris: None
bottleneck: None
dask: 2.21.0
distributed: 2.21.0
matplotlib: 3.3.0
cartopy: 0.18.0
seaborn: None
numbagg: None
pint: 0.14
setuptools: 49.2.0.post20200712
pip: 20.1.1
conda: None
pytest: 6.0.0
IPython: 7.16.1
sphinx: 2.4.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions