Skip to content

Commit

Permalink
Fix an overflow bug in decode_cf_datetime (#2015)
Browse files Browse the repository at this point in the history
* Fix an overflow bug in decode_cf_datetime

* Better test

* Other solution

* Better test

* Back to previous because of appveyor
  • Loading branch information
fmaussion authored and shoyer committed Mar 31, 2018
1 parent 1b48ac8 commit c78469a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Bug fixes
- Fixed labeled indexing with slice bounds given by xarray objects with
datetime64 or timedelta64 dtypes (:issue:`1240`).
By `Stephan Hoyer <https://github.com/shoyer>`_.
- Fixed a bug in decode_cf_datetime where ``int32`` arrays weren't parsed
correctly (:issue:`2002`).
By `Fabien Maussion <https://github.com/fmaussion>`_.

.. _whats-new.0.10.2:

Expand Down
3 changes: 2 additions & 1 deletion xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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') +
Expand Down
1 change: 1 addition & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c78469a

Please sign in to comment.