Skip to content

.sel() failures when using latest cftime release (v1.0.4) #3426

@andersy005

Description

@andersy005

I just updated to the latest cftime release, and all of a sudden sel() appears to be broken:

In [1]: import xarray as xr                                                                                                                        

In [2]: import cftime                                                                                                                              

In [3]: ds = xr.tutorial.load_dataset('rasm')                                                                                                      

In [4]: ds                                                                                                                                         
Out[4]: 
<xarray.Dataset>
Dimensions:  (time: 36, x: 275, y: 205)
Coordinates:
  * time     (time) object 1980-09-16 12:00:00 ... 1983-08-17 00:00:00
    xc       (y, x) float64 189.2 189.4 189.6 189.7 ... 17.65 17.4 17.15 16.91
    yc       (y, x) float64 16.53 16.78 17.02 17.27 ... 28.26 28.01 27.76 27.51
Dimensions without coordinates: x, y
Data variables:
    Tair     (time, y, x) float64 nan nan nan nan nan ... 29.8 28.66 28.19 28.21
Attributes:
    title:                     /workspace/jhamman/processed/R1002RBRxaaa01a/l...
    institution:               U.W.
    source:                    RACM R1002RBRxaaa01a
    output_frequency:          daily
    output_mode:               averaged
    convention:                CF-1.4
    references:                Based on the initial model of Liang et al., 19...
    comment:                   Output from the Variable Infiltration Capacity...
    nco_openmp_thread_number:  1
    NCO:                       "4.6.0"
    history:                   Tue Dec 27 14:15:22 2016: ncatted -a dimension...

In [5]: ds.sel(time=slice("1980", "1982"))                                                                                                         
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-2c26e36a673a> in <module>
----> 1 ds.sel(time=slice("1980", "1982"))

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1998         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
   1999         pos_indexers, new_indexes = remap_label_indexers(
-> 2000             self, indexers=indexers, method=method, tolerance=tolerance
   2001         )
   2002         result = self.isel(indexers=pos_indexers, drop=drop)

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    390 
    391     pos_indexers, new_indexes = indexing.remap_label_indexers(
--> 392         obj, v_indexers, method=method, tolerance=tolerance
    393     )
    394     # attach indexer's coordinate to pos_indexers

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    259             coords_dtype = data_obj.coords[dim].dtype
    260             label = maybe_cast_to_coords_dtype(label, coords_dtype)
--> 261             idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance)
    262             pos_indexers[dim] = idxr
    263             if new_idx is not None:

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
    123             _sanitize_slice_element(label.start),
    124             _sanitize_slice_element(label.stop),
--> 125             _sanitize_slice_element(label.step),
    126         )
    127         if not isinstance(indexer, slice):

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/pandas/core/indexes/base.py in slice_indexer(self, start, end, step, kind)
   5032         slice(1, 3)
   5033         """
-> 5034         start_slice, end_slice = self.slice_locs(start, end, step=step, kind=kind)
   5035 
   5036         # return a slice

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/pandas/core/indexes/base.py in slice_locs(self, start, end, step, kind)
   5246         start_slice = None
   5247         if start is not None:
-> 5248             start_slice = self.get_slice_bound(start, "left", kind)
   5249         if start_slice is None:
   5250             start_slice = 0

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_slice_bound(self, label, side, kind)
   5158         # For datetime indices label may be a string that has to be converted
   5159         # to datetime boundary according to its resolution.
-> 5160         label = self._maybe_cast_slice_bound(label, side, kind)
   5161 
   5162         # we need to look up the label

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in _maybe_cast_slice_bound(self, label, side, kind)
    336         pandas.tseries.index.DatetimeIndex._maybe_cast_slice_bound"""
    337         if isinstance(label, str):
--> 338             parsed, resolution = _parse_iso8601_with_reso(self.date_type, label)
    339             start, end = _parsed_string_to_bounds(self.date_type, resolution, parsed)
    340             if self.is_monotonic_decreasing and len(self) > 1:

~/opt/miniconda3/envs/intake-esm-dev/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in _parse_iso8601_with_reso(date_type, timestr)
    114     # 1.0.3.4.
    115     replace["dayofwk"] = -1
--> 116     return default.replace(**replace), resolution
    117 
    118 

cftime/_cftime.pyx in cftime._cftime.datetime.replace()

ValueError: Replacing the dayofyr or dayofwk of a datetime is not supported.

Output of xr.show_versions()

# Paste the output here xr.show_versions() here
In [6]: xr.show_versions()                                                                                                                         

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 14:38:56) 
[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: en_US.UTF-8
libhdf5: 1.10.5
libnetcdf: 4.7.1

xarray: 0.14.0
pandas: 0.25.2
numpy: 1.17.2
scipy: None
netCDF4: 1.5.1.2
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.3.2
cftime: 1.0.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.2.0
distributed: 2.5.1
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
setuptools: 41.4.0
pip: 19.2.1
conda: None
pytest: 5.0.1
IPython: 7.8.0
sphinx: 2.1.2

Expected Output

I can confirm that everything works just fine with an older version of cftime:

In [4]: ds.sel(time=slice("1980", "1982"))                                                                                                         
Out[4]: 
<xarray.Dataset>
Dimensions:  (time: 28, x: 275, y: 205)
Coordinates:
  * time     (time) object 1980-09-16 12:00:00 ... 1982-12-17 00:00:00
    xc       (y, x) float64 ...
    yc       (y, x) float64 ...
Dimensions without coordinates: x, y
Data variables:
    Tair     (time, y, x) float64 ...
Attributes:
    title:                     /workspace/jhamman/processed/R1002RBRxaaa01a/l...
    institution:               U.W.
    source:                    RACM R1002RBRxaaa01a
    output_frequency:          daily
    output_mode:               averaged
    convention:                CF-1.4
    references:                Based on the initial model of Liang et al., 19...
    comment:                   Output from the Variable Infiltration Capacity...
    nco_openmp_thread_number:  1
    NCO:                       "4.6.0"
    history:                   Tue Dec 27 14:15:22 2016: ncatted -a dimension...
In [5]: import cftime                                                                                                                              

In [6]: cftime.__version__                                                                                                                         
Out[6]: '1.0.3.4'

In [7]: xr.__version__                                                                                                                             
Out[7]: '0.14.0'

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