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

KeyError when faceting along time dimensions #4319

Closed
malmans2 opened this issue Aug 6, 2020 · 4 comments
Closed

KeyError when faceting along time dimensions #4319

malmans2 opened this issue Aug 6, 2020 · 4 comments

Comments

@malmans2
Copy link
Contributor

malmans2 commented Aug 6, 2020

What happened:
I think the latest pandas (1.1.0) conflicts with faceting along time dimensions.

Minimal Complete Verifiable Example:

import xarray as xr
airtemps = xr.tutorial.open_dataset("air_temperature") 
airtemps['air'].isel(time=slice(2)).plot(col='time') 

This returns the following error:

KeyError: 1356998400000000000

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: 5.4.0-42-generic
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: None
h5py: None
Nio: None
zarr: 2.4.0
cftime: 1.2.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.22.0
distributed: 2.22.0
matplotlib: 3.3.0
cartopy: 0.18.0
seaborn: None
numbagg: None
pint: None
setuptools: 49.2.1.post20200802
pip: 20.2.1
conda: None
pytest: 6.0.1
IPython: 7.17.0
sphinx: None

@malmans2
Copy link
Contributor Author

malmans2 commented Aug 6, 2020

Here is the full error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-c00f9ae5bb67> in <module>
----> 1 airtemps['air'].isel(time=slice(2)).plot(col='time')

~/anaconda3/envs/ospy_tests/lib/python3.7/site-packages/xarray/plot/plot.py in __call__(self, **kwargs)
    444 
    445     def __call__(self, **kwargs):
--> 446         return plot(self._da, **kwargs)
    447 
    448     # we can't use functools.wraps here since that also modifies the name / qualname

~/anaconda3/envs/ospy_tests/lib/python3.7/site-packages/xarray/plot/plot.py in plot(darray, row, col, col_wrap, ax, hue, rtol, subplot_kws, **kwargs)
    198     kwargs["ax"] = ax
    199 
--> 200     return plotfunc(darray, **kwargs)
    201 
    202 

~/anaconda3/envs/ospy_tests/lib/python3.7/site-packages/xarray/plot/plot.py in newplotfunc(darray, x, y, figsize, size, aspect, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, center, robust, extend, levels, infer_intervals, colors, subplot_kws, cbar_ax, cbar_kwargs, xscale, yscale, xticks, yticks, xlim, ylim, norm, **kwargs)
    636             # Need the decorated plotting function
    637             allargs["plotfunc"] = globals()[plotfunc.__name__]
--> 638             return _easy_facetgrid(darray, kind="dataarray", **allargs)
    639 
    640         plt = import_matplotlib_pyplot()

~/anaconda3/envs/ospy_tests/lib/python3.7/site-packages/xarray/plot/facetgrid.py in _easy_facetgrid(data, plotfunc, kind, x, y, row, col, col_wrap, sharex, sharey, aspect, size, subplot_kws, ax, figsize, **kwargs)
    642 
    643     if kind == "dataarray":
--> 644         return g.map_dataarray(plotfunc, x, y, **kwargs)
    645 
    646     if kind == "dataset":

~/anaconda3/envs/ospy_tests/lib/python3.7/site-packages/xarray/plot/facetgrid.py in map_dataarray(self, func, x, y, **kwargs)
    263         # Get x, y labels for the first subplot
    264         x, y = _infer_xy_labels(
--> 265             darray=self.data.loc[self.name_dicts.flat[0]],
    266             x=x,
    267             y=y,

~/anaconda3/envs/ospy_tests/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:

~/anaconda3/envs/ospy_tests/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)

~/anaconda3/envs/ospy_tests/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)

~/anaconda3/envs/ospy_tests/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

~/anaconda3/envs/ospy_tests/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:

~/anaconda3/envs/ospy_tests/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":

~/anaconda3/envs/ospy_tests/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: 1356998400000000000

@keewis
Copy link
Collaborator

keewis commented Aug 6, 2020

Thanks for the report, @malmans2. This is a general issue with sel / loc with datetime dtypes, see #4283 and #4292; we're working on this. Unfortunately, our test coverage was not complete enough for us to catch that before the pandas release.

For now, you might want to pin pandas to pandas<1.1.0.

@malmans2
Copy link
Contributor Author

malmans2 commented Aug 6, 2020

Got it! Thanks @keewis.

@dcherian
Copy link
Contributor

dcherian commented Aug 6, 2020

Closing as duplicate. Thanks @malmans2 for reporting.

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

3 participants