Skip to content

Commit

Permalink
simplify code, add whats-new.rst entry
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuehlbauer committed Feb 25, 2025
1 parent 03aa1b8 commit 31ed6bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Bug fixes
Haacker <https://github.com/j-haacker>`_.
- Fix ``isel`` for multi-coordinate Xarray indexes (:issue:`10063`, :pull:`10066`).
By `Benoit Bovy <https://github.com/benbovy>`_.
- Improve handling of dtype and NaT when encoding/decoding masked and packaged datetimes and timedeltas (:issue:`8957`, :pull:`10050`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.


Documentation
Expand Down
16 changes: 7 additions & 9 deletions xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,8 @@ def decode(self, variable: Variable, name: T_Name = None):
(is_time_like == "datetime" and self.decode_times)
or (is_time_like == "timedelta" and self.decode_timedelta)
) and data.dtype.kind in "iu":
dtype, decoded_fill_value = (
np.int64,
np.iinfo(np.int64).min,
) # np.dtype(f"{is_time_like}64[s]")
dtype = np.int64
decoded_fill_value = np.iinfo(np.int64).min
else:
dtype, decoded_fill_value = dtypes.maybe_promote(data.dtype)

Expand Down Expand Up @@ -624,17 +622,17 @@ def decode(self, variable: Variable, name: T_Name = None) -> Variable:
scale_factor = np.asarray(scale_factor).item()
if np.ndim(add_offset) > 0:
add_offset = np.asarray(add_offset).item()
# if we have a _FillValue/masked_value we already have the wanted
# if we have a _FillValue/masked_value in encoding we already have the wanted
# floating point dtype here (via CFMaskCoder), so no check is necessary
# only check in other cases and for time-like
dtype = data.dtype
is_time_like = _is_time_like(attrs.get("units"))
if (is_time_like == "datetime" and self.decode_times) or (
is_time_like == "timedelta" and self.decode_timedelta
if (
("_FillValue" not in encoding and "missing_value" not in encoding)
or (is_time_like == "datetime" and self.decode_times)
or (is_time_like == "timedelta" and self.decode_timedelta)
):
dtype = _choose_float_dtype(dtype, encoding)
if "_FillValue" not in encoding and "missing_value" not in encoding:
dtype = _choose_float_dtype(dtype, encoding)

transform = partial(
_scale_offset_decoding,
Expand Down

0 comments on commit 31ed6bb

Please sign in to comment.