Skip to content

Commit 7fcb80f

Browse files
spencerkclarkshoyer
authored andcommitted
Fix failure in time encoding for pandas < 0.21.1 (#2630)
* Fix failure in time encoding for pandas < 0.21.1 * Add a test
1 parent b5059a5 commit 7fcb80f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Bug fixes
8383
By `Martin Raspaud <https://github.com/mraspaud>`_.
8484
- Fix parsing of ``_Unsigned`` attribute set by OPENDAP servers. (:issue:`2583`).
8585
By `Deepak Cherian <https://github.com/dcherian>`_
86+
- Fix failure in time encoding when exporting to netCDF with versions of pandas
87+
less than 0.21.1 (:issue:`2623`). By `Spencer Clark
88+
<https://github.com/spencerkclark>`_.
8689
- Fix MultiIndex selection to update label and level (:issue:`2619`).
8790
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
8891

xarray/coding/times.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def encode_cf_datetime(dates, units=None, calendar=None):
357357

358358
delta_units = _netcdf_to_numpy_timeunit(delta)
359359
time_delta = np.timedelta64(1, delta_units).astype('timedelta64[ns]')
360-
ref_date = np.datetime64(pd.Timestamp(ref_date))
360+
ref_date = pd.Timestamp(ref_date)
361361

362362
# Wrap the dates in a DatetimeIndex to do the subtraction to ensure
363363
# an OverflowError is raised if the ref_date is too far away from

xarray/tests/test_coding_times.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,16 @@ def test_encode_cf_datetime_overflow(shape):
737737
num, _, _ = encode_cf_datetime(dates, units, calendar)
738738
roundtrip = decode_cf_datetime(num, units, calendar)
739739
np.testing.assert_array_equal(dates, roundtrip)
740+
741+
742+
def test_encode_cf_datetime_pandas_min():
743+
# Test that encode_cf_datetime does not fail for versions
744+
# of pandas < 0.21.1 (GH 2623).
745+
dates = pd.date_range('2000', periods=3)
746+
num, units, calendar = encode_cf_datetime(dates)
747+
expected_num = np.array([0., 1., 2.])
748+
expected_units = 'days since 2000-01-01 00:00:00'
749+
expected_calendar = 'proleptic_gregorian'
750+
np.testing.assert_array_equal(num, expected_num)
751+
assert units == expected_units
752+
assert calendar == expected_calendar

0 commit comments

Comments
 (0)