Skip to content

Commit eaa4a44

Browse files
committed
Take into account microsecond attribute in cftime_to_nptime
1 parent f65b764 commit eaa4a44

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

xarray/coding/times.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def cftime_to_nptime(times):
285285
# 1678 to 2262 (this is not currently the case for
286286
# datetime.datetime).
287287
dt = pd.Timestamp(t.year, t.month, t.day, t.hour, t.minute,
288-
t.second)
288+
t.second, t.microsecond)
289289
except ValueError as e:
290290
raise ValueError('Cannot convert date {} to a date in the '
291291
'standard calendar. Reason: {}.'.format(t, e))

xarray/tests/test_coding_times.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ def test_cf_datetime(num_dates, units, calendar):
8989
actual = coding.times.decode_cf_datetime(num_dates, units,
9090
calendar)
9191

92-
assert_array_equal(expected, actual)
92+
abs_diff = np.atleast_1d(abs(actual - expected)).astype(np.timedelta64)
93+
# once we no longer support versions of netCDF4 older than 1.1.5,
94+
# we could do this check with near microsecond accuracy:
95+
# https://github.com/Unidata/netcdf4-python/issues/355
96+
assert (abs_diff <= np.timedelta64(1, 's')).all()
9397
encoded, _, _ = coding.times.encode_cf_datetime(actual, units,
9498
calendar)
9599
if '1-1-1' not in units:
@@ -151,7 +155,11 @@ def test_decode_cf_datetime_non_iso_strings():
151155
(np.arange(100), 'hours since 2000-01-01 0:00')]
152156
for num_dates, units in cases:
153157
actual = coding.times.decode_cf_datetime(num_dates, units)
154-
assert_array_equal(actual, expected)
158+
abs_diff = abs(actual - expected)
159+
# once we no longer support versions of netCDF4 older than 1.1.5,
160+
# we could do this check with near microsecond accuracy:
161+
# https://github.com/Unidata/netcdf4-python/issues/355
162+
assert (abs_diff <= np.timedelta64(1, 's')).all()
155163

156164

157165
@pytest.mark.skipif(not has_cftime_or_netCDF4, reason='cftime not installed')

0 commit comments

Comments
 (0)