From b3c6f8ccd8d9a2afa3a29f983b8f476646896382 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Mon, 24 Dec 2018 07:51:51 -0500 Subject: [PATCH 1/2] Fix failure in time encoding for pandas < 0.21.1 --- doc/whats-new.rst | 4 +++- xarray/coding/times.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 99a7d33226b..9bba7fd7fb1 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -83,7 +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 + `_. .. _whats-new.0.11.0: 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 From af0b445efa271db577d165b5579a2f9090c89aca Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Mon, 24 Dec 2018 08:54:34 -0500 Subject: [PATCH 2/2] Add a test --- xarray/tests/test_coding_times.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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