Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Bug Fixes
By `Justus Magin <https://github.com/keewis>`_.
- Avoid casting custom indexes in ``Dataset.drop_attrs`` (:pull:`10961`)
By `Justus Magin <https://github.com/keewis>`_.
- Support decoding unsigned integers to ``np.timedelta64``.
By `Deepak Cherian <https://github.com/dcherian>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ def _numbers_to_timedelta(
nan = np.asarray(np.isnan(flat_num))
elif flat_num.dtype.kind == "i":
nan = np.asarray(flat_num == np.iinfo(np.int64).min)
elif flat_num.dtype.kind == "u":
nan = np.broadcast_to(np.asarray(False), flat_num.shape)

# in case we need to change the unit, we fix the numbers here
# this should be safe, as errors would have been raised above
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,15 @@ def test_decode_timedelta_via_dtype(
assert decoded.dtype == expected_dtype


@pytest.mark.parametrize("dtype", [np.uint64, np.int64, np.float64])
def test_decode_timedelta_dtypes(dtype) -> None:
encoded = Variable(["time"], np.arange(10), {"units": "seconds"})
coder = CFTimedeltaCoder(time_unit="s")
decoded = coder.decode(encoded)
assert decoded.dtype.kind == "m"
assert_equal(coder.encode(decoded), encoded)


def test_lazy_decode_timedelta_unexpected_dtype() -> None:
attrs = {"units": "seconds"}
encoded = Variable(["time"], [0, 0.5, 1], attrs=attrs)
Expand Down
Loading