diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 3b5baeaa3cb..6a37e0c0833 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -83,6 +83,9 @@ Bug fixes By `Martin Raspaud `_. - Fix parsing of ``_Unsigned`` attribute set by OPENDAP servers. (:issue:`2583`). By `Deepak Cherian `_ +- Fix failure in time encoding when exporting to netCDF with versions of pandas + less than 0.21.1 (:issue:`2623`). By `Spencer Clark + `_. - Fix MultiIndex selection to update label and level (:issue:`2619`). By `Keisuke Fujii `_. diff --git a/xarray/coding/times.py b/xarray/coding/times.py index dfc4b2fb023..0f2045cf356 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -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 diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 5b69d9adcc0..b7db2b43cab 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -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