Skip to content

Commit

Permalink
Fix handling of abbreviated units like msec (#3998)
Browse files Browse the repository at this point in the history
* Fix handling of abbreviated units like msec

By default, xarray tries to decode times with pandas and falls back to
cftime. This fixes the exception handler to fallback properly in the
cases an unhandled abbreviated unit is passed in.

* Add what's new entry
  • Loading branch information
dopplershift authored Apr 24, 2020
1 parent 6ca3bd7 commit 33a66d6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ Bug fixes
- Fix bug causing :py:meth:`DataArray.interpolate_na` to always drop attributes,
and added `keep_attrs` argument. (:issue:`3968`)
By `Tom Nicholas <https://github.com/TomNicholas>`_.

- Fix bug in time parsing failing to fall back to cftime. This was causing time
variables with a time unit of `'msecs'` to fail to parse. (:pull:`3998`)
By `Ryan May <https://github.com/dopplershift>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def decode_cf_datetime(num_dates, units, calendar=None, use_cftime=None):
if use_cftime is None:
try:
dates = _decode_datetime_with_pandas(flat_num_dates, units, calendar)
except (OutOfBoundsDatetime, OverflowError):
except (KeyError, OutOfBoundsDatetime, OverflowError):
dates = _decode_datetime_with_cftime(
flat_num_dates.astype(np.float), units, calendar
)
Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ def test_decode_360_day_calendar():
assert_array_equal(actual, expected)


@requires_cftime
def test_decode_abbreviation():
"""Test making sure we properly fall back to cftime on abbreviated units."""
import cftime

val = np.array([1586628000000.0])
units = "msecs since 1970-01-01T00:00:00Z"
actual = coding.times.decode_cf_datetime(val, units)
expected = coding.times.cftime_to_nptime(cftime.num2date(val, units))
assert_array_equal(actual, expected)


@arm_xfail
@requires_cftime
@pytest.mark.parametrize(
Expand Down

0 comments on commit 33a66d6

Please sign in to comment.