diff --git a/doc/whats-new.rst b/doc/whats-new.rst index fbd2617b16f..77d6296acac 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -74,6 +74,7 @@ Deprecations Bug fixes ~~~~~~~~~ +- Ensure standard calendar times encoded with large values (i.e. greater than approximately 292 years), can be decoded correctly without silently overflowing (:pull:`5050`). This was a regression in xarray 0.17.0. By `Zeb Nicholls `_. - Added support for `numpy.bool_` attributes in roundtrips using `h5netcdf` engine with `invalid_netcdf=True` [which casts `bool`s to `numpy.bool_`] (:issue:`4981`, :pull:`4986`). By `Victor Negîrneac `_. - Don't allow passing ``axis`` to :py:meth:`Dataset.reduce` methods (:issue:`3510`, :pull:`4940`). diff --git a/xarray/coding/times.py b/xarray/coding/times.py index a1822393dc1..ff42e4e8973 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -181,6 +181,11 @@ def _decode_datetime_with_pandas(flat_num_dates, units, calendar): # strings, in which case we fall back to using cftime raise OutOfBoundsDatetime + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "invalid value encountered", RuntimeWarning) + pd.to_timedelta(flat_num_dates.min(), delta) + ref_date + pd.to_timedelta(flat_num_dates.max(), delta) + ref_date + # To avoid integer overflow when converting to nanosecond units for integer # dtypes smaller than np.int64 cast all integer-dtype arrays to np.int64 # (GH 2002). diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index eda32d31148..ad0ff9249fc 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -79,6 +79,9 @@ (0, "microseconds since 2000-01-01T00:00:00"), (np.int32(788961600), "seconds since 1981-01-01"), # GH2002 (12300 + np.arange(5), "hour since 1680-01-01 00:00:00.500000"), + (164375, "days since 1850-01-01 00:00:00"), + (164374.5, "days since 1850-01-01 00:00:00"), + ([164374.5, 168360.5], "days since 1850-01-01 00:00:00"), ] _CF_DATETIME_TESTS = [ num_dates_units + (calendar,)