Skip to content

Commit

Permalink
Take into account microsecond attribute in cftime_to_nptime
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerkclark committed Oct 28, 2018
1 parent f65b764 commit eaa4a44
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 10 additions & 2 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit eaa4a44

Please sign in to comment.