Skip to content

Commit

Permalink
Fix failure in time encoding for pandas < 0.21.1 (#2630)
Browse files Browse the repository at this point in the history
* Fix failure in time encoding for pandas < 0.21.1

* Add a test
  • Loading branch information
spencerkclark authored and shoyer committed Dec 24, 2018
1 parent b5059a5 commit 7fcb80f
Show file tree
Hide file tree
Showing 3 changed files with 17 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 @@ -83,6 +83,9 @@ Bug fixes
By `Martin Raspaud <https://github.com/mraspaud>`_.
- Fix parsing of ``_Unsigned`` attribute set by OPENDAP servers. (:issue:`2583`).
By `Deepak Cherian <https://github.com/dcherian>`_
- Fix failure in time encoding when exporting to netCDF with versions of pandas
less than 0.21.1 (:issue:`2623`). By `Spencer Clark
<https://github.com/spencerkclark>`_.
- Fix MultiIndex selection to update label and level (:issue:`2619`).
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

Expand Down
2 changes: 1 addition & 1 deletion xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def encode_cf_datetime(dates, units=None, calendar=None):

delta_units = _netcdf_to_numpy_timeunit(delta)
time_delta = np.timedelta64(1, delta_units).astype('timedelta64[ns]')
ref_date = np.datetime64(pd.Timestamp(ref_date))
ref_date = pd.Timestamp(ref_date)

# Wrap the dates in a DatetimeIndex to do the subtraction to ensure
# an OverflowError is raised if the ref_date is too far away from
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,16 @@ def test_encode_cf_datetime_overflow(shape):
num, _, _ = encode_cf_datetime(dates, units, calendar)
roundtrip = decode_cf_datetime(num, units, calendar)
np.testing.assert_array_equal(dates, roundtrip)


def test_encode_cf_datetime_pandas_min():
# Test that encode_cf_datetime does not fail for versions
# of pandas < 0.21.1 (GH 2623).
dates = pd.date_range('2000', periods=3)
num, units, calendar = encode_cf_datetime(dates)
expected_num = np.array([0., 1., 2.])
expected_units = 'days since 2000-01-01 00:00:00'
expected_calendar = 'proleptic_gregorian'
np.testing.assert_array_equal(num, expected_num)
assert units == expected_units
assert calendar == expected_calendar

0 comments on commit 7fcb80f

Please sign in to comment.