Skip to content

Slicing bug with pandas 1.3 and CFTimeIndex #5576

Closed
@huard

Description

@huard

What happened:
Slicing into a DataArray along time with a CFTimeIndex fails since upgrade to pandas 1.3

What you expected to happen:
The usual.

Minimal Complete Verifiable Example:

import xarray as xr
t = xr.cftime_range('2000-01-01', '2030-12-31', freq='D', calendar='noleap')
ref = xr.DataArray(range(len(t)), dims=('time',), coords={'time': t})
ref.sel(time=slice(None, "2015-01-01"))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-3afe7d577940> in <module>
----> 1 ref.sel(time=slice(None, "2015-01-01"))

~/.conda/envs/xclim/lib/python3.8/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1269         Dimensions without coordinates: points
   1270         """
-> 1271         ds = self._to_temp_dataset().sel(
   1272             indexers=indexers,
   1273             drop=drop,

~/.conda/envs/xclim/lib/python3.8/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2363         """
   2364         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
-> 2365         pos_indexers, new_indexes = remap_label_indexers(
   2366             self, indexers=indexers, method=method, tolerance=tolerance
   2367         )

~/.conda/envs/xclim/lib/python3.8/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    419     }
    420 
--> 421     pos_indexers, new_indexes = indexing.remap_label_indexers(
    422         obj, v_indexers, method=method, tolerance=tolerance
    423     )

~/.conda/envs/xclim/lib/python3.8/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    272             coords_dtype = data_obj.coords[dim].dtype
    273             label = maybe_cast_to_coords_dtype(label, coords_dtype)
--> 274             idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance)
    275             pos_indexers[dim] = idxr
    276             if new_idx is not None:

~/.conda/envs/xclim/lib/python3.8/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
    119                 "cannot use ``method`` argument if any indexers are slice objects"
    120             )
--> 121         indexer = index.slice_indexer(
    122             _sanitize_slice_element(label.start),
    123             _sanitize_slice_element(label.stop),

~/.conda/envs/xclim/lib/python3.8/site-packages/pandas/core/indexes/base.py in slice_indexer(self, start, end, step, kind)
   5684         slice(1, 3, None)
   5685         """
-> 5686         start_slice, end_slice = self.slice_locs(start, end, step=step)
   5687 
   5688         # return a slice

~/.conda/envs/xclim/lib/python3.8/site-packages/pandas/core/indexes/base.py in slice_locs(self, start, end, step, kind)
   5892         end_slice = None
   5893         if end is not None:
-> 5894             end_slice = self.get_slice_bound(end, "right")
   5895         if end_slice is None:
   5896             end_slice = len(self)

~/.conda/envs/xclim/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind)
   5796         # For datetime indices label may be a string that has to be converted
   5797         # to datetime boundary according to its resolution.
-> 5798         label = self._maybe_cast_slice_bound(label, side)
   5799 
   5800         # we need to look up the label

TypeError: _maybe_cast_slice_bound() missing 1 required positional argument: 'kind'

Anything else we need to know?:
A quick diagnostic suggests that convert_label_indexer should call pandas.Index.slice_indexer with a kind argument.

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 23:21:18)
[GCC 9.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-77-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: ('en_CA', 'UTF-8')
libhdf5: 1.10.6
libnetcdf: 4.7.4

xarray: 0.18.2
pandas: 1.3.0
numpy: 1.20.0
scipy: 1.6.3
netCDF4: 1.5.5.1
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.4.1
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2021.01.1
distributed: 2021.01.1
matplotlib: 3.4.2
cartopy: None
seaborn: None
numbagg: None
pint: 0.16.1
setuptools: 49.6.0.post20210108
pip: 21.0.1
conda: None
pytest: 6.2.2
IPython: 7.20.0
sphinx: 4.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions