diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b28beb9e3b2..87b019b54aa 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -47,6 +47,9 @@ Bug fixes - Fixed labeled indexing with slice bounds given by xarray objects with datetime64 or timedelta64 dtypes (:issue:`1240`). By `Stephan Hoyer `_. +- Fixed a bug in decode_cf_datetime where ``int32`` arrays weren't parsed + correctly (:issue:`2002`). + By `Fabien Maussion `_. .. _whats-new.0.10.2: diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 1bb4e31ae7e..8a1e9f82c6c 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -166,7 +166,8 @@ def decode_cf_datetime(num_dates, units, calendar=None): # Cast input dates to integers of nanoseconds because `pd.to_datetime` # works much faster when dealing with integers - flat_num_dates_ns_int = (flat_num_dates * + # make _NS_PER_TIME_DELTA an array to ensure type upcasting + flat_num_dates_ns_int = (flat_num_dates.astype(np.float64) * _NS_PER_TIME_DELTA[delta]).astype(np.int64) dates = (pd.to_timedelta(flat_num_dates_ns_int, 'ns') + diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index b85f92ece66..ab33329b51a 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -49,6 +49,7 @@ def test_cf_datetime(self): ([0.5, 1.5], 'hours since 1900-01-01T00:00:00'), (0, 'milliseconds since 2000-01-01T00:00:00'), (0, 'microseconds since 2000-01-01T00:00:00'), + (np.int32(788961600), 'seconds since 1981-01-01'), # GH2002 ]: for calendar in ['standard', 'gregorian', 'proleptic_gregorian']: expected = _ensure_naive_tz(