From eaa4a44cace084a43d59bf1e3aa3c7b57d39bbe2 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sun, 28 Oct 2018 09:29:54 -0400 Subject: [PATCH] Take into account microsecond attribute in cftime_to_nptime --- xarray/coding/times.py | 2 +- xarray/tests/test_coding_times.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index d4cd88431a0..aa825b88fe3 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -285,7 +285,7 @@ def cftime_to_nptime(times): # 1678 to 2262 (this is not currently the case for # datetime.datetime). dt = pd.Timestamp(t.year, t.month, t.day, t.hour, t.minute, - t.second) + t.second, t.microsecond) except ValueError as e: raise ValueError('Cannot convert date {} to a date in the ' 'standard calendar. Reason: {}.'.format(t, e)) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 3b515aa6871..cf7d63a30f3 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -89,7 +89,11 @@ def test_cf_datetime(num_dates, units, calendar): actual = coding.times.decode_cf_datetime(num_dates, units, calendar) - assert_array_equal(expected, actual) + abs_diff = np.atleast_1d(abs(actual - expected)).astype(np.timedelta64) + # once we no longer support versions of netCDF4 older than 1.1.5, + # we could do this check with near microsecond accuracy: + # https://github.com/Unidata/netcdf4-python/issues/355 + assert (abs_diff <= np.timedelta64(1, 's')).all() encoded, _, _ = coding.times.encode_cf_datetime(actual, units, calendar) if '1-1-1' not in units: @@ -151,7 +155,11 @@ def test_decode_cf_datetime_non_iso_strings(): (np.arange(100), 'hours since 2000-01-01 0:00')] for num_dates, units in cases: actual = coding.times.decode_cf_datetime(num_dates, units) - assert_array_equal(actual, expected) + abs_diff = abs(actual - expected) + # once we no longer support versions of netCDF4 older than 1.1.5, + # we could do this check with near microsecond accuracy: + # https://github.com/Unidata/netcdf4-python/issues/355 + assert (abs_diff <= np.timedelta64(1, 's')).all() @pytest.mark.skipif(not has_cftime_or_netCDF4, reason='cftime not installed')