Skip to content

Commit fbd48d4

Browse files
authored
Fix regression in decoding large standard calendar times (#5050)
1 parent a6f51c6 commit fbd48d4

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/whats-new.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Deprecations
7474

7575
Bug fixes
7676
~~~~~~~~~
77+
- 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 <https://github.com/znicholls>`_.
7778
- 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`).
7879
By `Victor Negîrneac <https://github.com/caenrigen>`_.
7980
- Don't allow passing ``axis`` to :py:meth:`Dataset.reduce` methods (:issue:`3510`, :pull:`4940`).

xarray/coding/times.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ def _decode_datetime_with_pandas(flat_num_dates, units, calendar):
181181
# strings, in which case we fall back to using cftime
182182
raise OutOfBoundsDatetime
183183

184+
with warnings.catch_warnings():
185+
warnings.filterwarnings("ignore", "invalid value encountered", RuntimeWarning)
186+
pd.to_timedelta(flat_num_dates.min(), delta) + ref_date
187+
pd.to_timedelta(flat_num_dates.max(), delta) + ref_date
188+
184189
# To avoid integer overflow when converting to nanosecond units for integer
185190
# dtypes smaller than np.int64 cast all integer-dtype arrays to np.int64
186191
# (GH 2002).

xarray/tests/test_coding_times.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
(0, "microseconds since 2000-01-01T00:00:00"),
8080
(np.int32(788961600), "seconds since 1981-01-01"), # GH2002
8181
(12300 + np.arange(5), "hour since 1680-01-01 00:00:00.500000"),
82+
(164375, "days since 1850-01-01 00:00:00"),
83+
(164374.5, "days since 1850-01-01 00:00:00"),
84+
([164374.5, 168360.5], "days since 1850-01-01 00:00:00"),
8285
]
8386
_CF_DATETIME_TESTS = [
8487
num_dates_units + (calendar,)

0 commit comments

Comments
 (0)