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 1421718311,I_kwDOAMm_X85UvbMn,7210,Unable to convert times to datetime when first and last value are masked,11022336,open,0,,,2,2022-10-25T01:57:04Z,2022-10-27T15:30:57Z,,NONE,,,,"### What happened? In the following conditions, I am unable to open a NetCDF file in xarray with `mask_and_scale=True` and `decode_times=True`: * Unusual timestamp format in `time` variable `units` attr, e.g. `seconds since 2000-1-1 0:0:0 0` * First and last value in `time` matches `_FillValue` The following changes to the NetCDF file allows me to open the file in xarray: * Update timestamp format in `units` attr to something more common (e.g. `seconds since 2000-01-01T00:00:00`) OR * Update first and last value in `time` variable to value that is not equal to `_FillValue` ### What did you expect to happen? I should be able to open the NetCDF file with `mask_and_scale=True` and `decode_times=True` without errors. ### Minimal Complete Verifiable Example ```Python import xarray as xr import numpy as np fill_val = -99999.0 time_vals = np.random.rand(10) time_vals[0] = fill_val time_vals[-1] = fill_val data_vars = { 'foo': (['x'], np.random.rand(10)), 'time': ( ['x'], time_vals, { 'units': 'seconds since 2000-1-1 0:0:0 0', '_FillValue': fill_val, 'standard_name': 'time', 'calendar': 'standard' } ), } ds = xr.Dataset( data_vars=data_vars, coords={'x': (['x'], np.arange(10))} ) nc_out_location = '/path/to/example.nc' ds.to_netcdf(nc_out_location) ds = xr.open_dataset(nc_out_location) # This line results in an error ``` ### MVCE confirmation - [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray. - [X] Complete example — the example is self-contained, including all data and the text of any traceback. - [X] Verifiable example — the example copy & pastes into an IPython prompt or [Binder notebook](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result. - [X] New issue — a search of GitHub Issues suggests this is not a duplicate. ### Relevant log output ```Python dateutil.parser._parser.ParserError: Unknown string format: 2000-1-1 0:0:0 0 ... ValueError: could not convert string to Timestamp ... pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime ... TypeError: unsupported operand type(s) for +: 'cftime._cftime.DatetimeGregorian' and 'NoneType' ... ValueError: unable to decode time units 'seconds since 2000-1-1 0:0:0 0' with ""calendar 'standard'"". Try opening your dataset with decode_times=False or installing cftime if it is not installed. ``` ### Anything else we need to know? Looking at the code, it seems like this is happening because `_decode_cf_datetime_dtype` in times.py is only selecting the first and last values from the data, which in this case are masked due to _FillValue. ### Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.9.1 (default, Jan 8 2021, 17:17:17) [Clang 12.0.0 (clang-1200.0.32.28)] python-bits: 64 OS: Darwin OS-release: 21.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.0 libnetcdf: 4.7.4 xarray: 2022.10.0 pandas: 1.4.1 numpy: 1.22.3 scipy: 1.8.0 netCDF4: 1.5.7 pydap: None h5netcdf: None h5py: None Nio: None zarr: None cftime: 1.5.1 nc_time_axis: None PseudoNetCDF: None rasterio: 1.2.10 cfgrib: None iris: None bottleneck: None dask: 2022.02.0 distributed: 2022.02.0 matplotlib: 3.5.1 cartopy: None seaborn: None numbagg: None fsspec: 2022.01.0 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 60.10.0 pip: 21.1.1 conda: None pytest: None IPython: None sphinx: None
","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/7210/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,issue