Skip to content

Commit 66e4309

Browse files
authored
Support decoding unsigned integers to timedelta (#10972)
1 parent b3f5db3 commit 66e4309

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Bug Fixes
4646
By `Justus Magin <https://github.com/keewis>`_.
4747
- Avoid casting custom indexes in ``Dataset.drop_attrs`` (:pull:`10961`)
4848
By `Justus Magin <https://github.com/keewis>`_.
49+
- Support decoding unsigned integers to ``np.timedelta64``.
50+
By `Deepak Cherian <https://github.com/dcherian>`_.
51+
4952

5053
Documentation
5154
~~~~~~~~~~~~~

xarray/coding/times.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ def _numbers_to_timedelta(
626626
nan = np.asarray(np.isnan(flat_num))
627627
elif flat_num.dtype.kind == "i":
628628
nan = np.asarray(flat_num == np.iinfo(np.int64).min)
629+
elif flat_num.dtype.kind == "u":
630+
nan = np.broadcast_to(np.asarray(False), flat_num.shape)
629631

630632
# in case we need to change the unit, we fix the numbers here
631633
# this should be safe, as errors would have been raised above

xarray/tests/test_coding_times.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,15 @@ def test_decode_timedelta_via_dtype(
19501950
assert decoded.dtype == expected_dtype
19511951

19521952

1953+
@pytest.mark.parametrize("dtype", [np.uint64, np.int64, np.float64])
1954+
def test_decode_timedelta_dtypes(dtype) -> None:
1955+
encoded = Variable(["time"], np.arange(10), {"units": "seconds"})
1956+
coder = CFTimedeltaCoder(time_unit="s")
1957+
decoded = coder.decode(encoded)
1958+
assert decoded.dtype.kind == "m"
1959+
assert_equal(coder.encode(decoded), encoded)
1960+
1961+
19531962
def test_lazy_decode_timedelta_unexpected_dtype() -> None:
19541963
attrs = {"units": "seconds"}
19551964
encoded = Variable(["time"], [0, 0.5, 1], attrs=attrs)

0 commit comments

Comments
 (0)