id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 1130073503,I_kwDOAMm_X85DW42f,6263,"Date in matplotlib conversion does not handle ""YYYY-MM-DD"" format for xarray=0.21.1",5339008,open,0,,,3,2022-02-10T13:21:59Z,2022-02-23T18:09:32Z,,NONE,,,,"### What happened? When setting axis limits is in matplotlib.: ax.set_xlim([""2002-01-03"",""2002-01-20""]) A conversion error is raised. --------------------------------------------------------------------------- IndexError Traceback (most recent call last) File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\axis.py:1507, in Axis.convert_units(self, x) 1506 try: -> 1507 ret = self.converter.convert(x, self.units, self) 1508 except Exception as e: File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\dates.py:1921, in _SwitchableDateConverter.convert(self, *args, **kwargs) 1920 def convert(self, *args, **kwargs): -> 1921 return self._get_converter().convert(*args, **kwargs) File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\dates.py:1850, in DateConverter.convert(value, unit, axis) 1844 """""" 1845 If *value* is not already a number or sequence of numbers, convert it 1846 with `date2num`. 1847 1848 The *unit* and *axis* arguments are not used. 1849 """""" -> 1850 return date2num(value) File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\dates.py:443, in date2num(d) 441 return d --> 443 tzi = getattr(d[0], 'tzinfo', None) 444 if tzi is not None: 445 # make datetime naive: IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed The above exception was the direct cause of the following exception: ConversionError Traceback (most recent call last) Input In [4], in 10 fig, ax = plt.subplots() 11 ax.plot(times, y) ---> 12 ax.set_xlim([""2002-01-03"",""2002-01-20""]) File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\axes\_base.py:3689, in _AxesBase.set_xlim(self, left, right, emit, auto, xmin, xmax) 3686 right = xmax 3688 self._process_unit_info([(""x"", (left, right))], convert=False) -> 3689 left = self._validate_converted_limits(left, self.convert_xunits) 3690 right = self._validate_converted_limits(right, self.convert_xunits) 3692 if left is None or right is None: 3693 # Axes init calls set_xlim(0, 1) before get_xlim() can be called, 3694 # so only grab the limits if we really need them. File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\axes\_base.py:3603, in _AxesBase._validate_converted_limits(self, limit, convert) 3592 """""" 3593 Raise ValueError if converted limits are non-finite. 3594 (...) 3599 The limit value after call to convert(), or None if limit is None. 3600 """""" 3601 if limit is not None: -> 3603 converted_limit = convert(limit) 3604 if (isinstance(converted_limit, Real) 3605 and not np.isfinite(converted_limit)): 3606 raise ValueError(""Axis limits cannot be NaN or Inf"") File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\artist.py:252, in Artist.convert_xunits(self, x) 250 if ax is None or ax.xaxis is None: 251 return x --> 252 return ax.xaxis.convert_units(x) File C:\work\miniconda3\envs\XROMStest\lib\site-packages\matplotlib\axis.py:1509, in Axis.convert_units(self, x) 1507 ret = self.converter.convert(x, self.units, self) 1508 except Exception as e: -> 1509 raise munits.ConversionError('Failed to convert value(s) to axis ' 1510 f'units: {x!r}') from e 1511 return ret ConversionError: Failed to convert value(s) to axis units: '2002-01-03' When we downgrade to xarray=0.20..2. axis limits are changed as expected. ### What did you expect to happen? ax.set_xlim should interpert dates of format ""YYYY-MM-DD"" correctly and scale the xaxis appropriately. ### Minimal Complete Verifiable Example ```python import xarray as xr import numpy as np import matplotlib.pyplot as plt times = np.arange(np.datetime64('2001-01-02'), np.datetime64('2002-02-03'), np.timedelta64(75, 'm')) y = np.random.randn(len(times)) fig, ax = plt.subplots() ax.plot(times, y) ax.set_xlim([""2002-01-03"",""2002-01-20""]) ``` ### Relevant log output _No response_ ### Anything else we need to know? It appears that matplotlib uses a pandas date converter when using xarray 0.20.2 is imported but not for xarray 0.21.1. ### Environment INSTALLED VERSIONS ------------------ commit: None python: 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:22:46) [MSC v.1916 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 140 Stepping 1, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: ('English_United States', '1252') libhdf5: 1.12.1 libnetcdf: 4.8.1 xarray: 0.21.1 pandas: 1.4.0 numpy: 1.21.5 scipy: 1.8.0 netCDF4: 1.5.8 pydap: None h5netcdf: None h5py: None Nio: None zarr: 2.11.0 cftime: 1.5.2 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: None iris: None bottleneck: None dask: 2022.01.1 distributed: 2022.01.1 matplotlib: 3.5.1 cartopy: 0.20.2 seaborn: None numbagg: None fsspec: 2022.01.0 cupy: None pint: None sparse: None setuptools: 59.8.0 pip: 22.0.3 conda: None pytest: None IPython: 8.0.1 sphinx: None","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/6263/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,issue 830930964,MDU6SXNzdWU4MzA5MzA5NjQ=,5032,Error when using xarray where that results in zero dimensions. ,5339008,closed,0,,,4,2021-03-13T16:09:30Z,2021-04-19T02:48:39Z,2021-04-19T02:48:38Z,NONE,,,,"Xarray 0.17.0 Executing the following code: ```python import xarray as xray ncurl='http://tds.coaps.fsu.edu/thredds/dodsC/samos/data/intermediate/ZMFR/2021/ZMFR_20210303v20001.nc' dat =xray.open_dataset(ncurl) #dat=dat.where(dat.lat>-90.0,drop=True) dat=dat.where(dat.lat>-11.0,drop=True) dat=dat.where(dat.lat<30.0,drop=True) dat=dat.where(dat.lon>91.0,drop=True) dat=dat.where(dat.lon<139.0,drop=True) print(dat) ``` Results in the error: ```python Traceback (most recent call last): File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/backends/netCDF4_.py"", line 98, in _getitem array = getitem(original_array, key) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/backends/common.py"", line 53, in robust_getitem return array[key] File ""src/netCDF4/_netCDF4.pyx"", line 4397, in netCDF4._netCDF4.Variable.__getitem__ File ""/home/hunter/miniconda3/lib/python3.8/site-packages/netCDF4/utils.py"", line 467, in _out_array_shape c = count[..., i].ravel()[0] # All elements should be identical. IndexError: index 0 is out of bounds for axis 0 with size 0 During handling of the above exception, another exception occurred: Traceback (most recent call last): File ""./test_xarray_error.py"", line 7, in dat=dat.where(dat.lat>-11.0,drop=True) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/common.py"", line 1273, in where return ops.where_method(self, cond, other) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/ops.py"", line 195, in where_method return apply_ufunc( File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/computation.py"", line 1116, in apply_ufunc return apply_dataset_vfunc( File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/computation.py"", line 428, in apply_dataset_vfunc result_vars = apply_dict_of_variables_vfunc( File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/computation.py"", line 373, in apply_dict_of_variables_vfunc result_vars[name] = func(*variable_args) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/computation.py"", line 628, in apply_variable_ufunc input_data = [ File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/computation.py"", line 629, in broadcast_compat_data(arg, broadcast_dims, core_dims) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/computation.py"", line 542, in broadcast_compat_data data = variable.data File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/variable.py"", line 374, in data return self.values File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/variable.py"", line 554, in values return _as_array_or_item(self._data) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/variable.py"", line 287, in _as_array_or_item data = np.asarray(data) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/numpy/core/_asarray.py"", line 102, in asarray return array(a, dtype, copy=False, order=order) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/indexing.py"", line 693, in __array__ self._ensure_cached() File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/indexing.py"", line 690, in _ensure_cached self.array = NumpyIndexingAdapter(np.asarray(self.array)) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/numpy/core/_asarray.py"", line 102, in asarray return array(a, dtype, copy=False, order=order) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/indexing.py"", line 663, in __array__ return np.asarray(self.array, dtype=dtype) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/numpy/core/_asarray.py"", line 102, in asarray return array(a, dtype, copy=False, order=order) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/indexing.py"", line 568, in __array__ return np.asarray(array[self.key], dtype=None) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/coding/strings.py"", line 236, in __getitem__ return _numpy_char_to_bytes(self.array[key]) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/coding/strings.py"", line 193, in _numpy_char_to_bytes arr = np.array(arr, copy=False, order=""C"") File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/indexing.py"", line 568, in __array__ return np.asarray(array[self.key], dtype=None) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/backends/netCDF4_.py"", line 85, in __getitem__ return indexing.explicit_indexing_adapter( File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/core/indexing.py"", line 853, in explicit_indexing_adapter result = raw_indexing_method(raw_key.tuple) File ""/home/hunter/miniconda3/lib/python3.8/site-packages/xarray/backends/netCDF4_.py"", line 108, in _getitem raise IndexError(msg) IndexError: The indexing operation you are attempting to perform is not valid on netCDF4.Variable object. Try loading your data into memory first by calling .load(). ``` It seems to be related to the fact that: `dat=dat.where(dat.lat>-11.0,drop=True)` results in a zero dimension (time). However, if I uncomment one line and run: ```python import xarray as xray ncurl='http://tds.coaps.fsu.edu/thredds/dodsC/samos/data/intermediate/ZMFR/2021/ZMFR_20210303v20001.nc' dat =xray.open_dataset(ncurl) dat=dat.where(dat.lat>-90.0,drop=True) dat=dat.where(dat.lat>-11.0,drop=True) dat=dat.where(dat.lat<30.0,drop=True) dat=dat.where(dat.lon>91.0,drop=True) dat=dat.where(dat.lon<139.0,drop=True) print(dat) ``` There is no error, even though the line with `dat=dat.where(dat.lat>-11.0,drop=True)` still results in a zero dimension. Perhaps this is not a bug, but maybe I am misunderstanding how where() works in xarray. But it seems inconsistent. Eli Hunter Rutgers University","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/5032/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue