Skip to content

Commit

Permalink
remove dtype from encoding for datetime64/timedelta64 variables to …
Browse files Browse the repository at this point in the history
…prevent unnecessary casts
  • Loading branch information
kmuehlbauer committed Sep 19, 2023
1 parent b08a9d8 commit 37db2b0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
safe_setitem(attrs, "units", units, name=name)
safe_setitem(attrs, "calendar", calendar, name=name)

# drop dtype in encoding if encoded as float64
# to prevent unnecessary casts, see GH #1064
if np.issubdtype(data.dtype, np.float64):
encoding.pop("dtype", None)

return Variable(dims, data, attrs, encoding, fastpath=True)
else:
return variable
Expand Down Expand Up @@ -810,6 +815,11 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
data, units = encode_cf_timedelta(data, encoding.pop("units", None))
safe_setitem(attrs, "units", units, name=name)

# drop dtype in encoding if encoded as float64
# to prevent unnecessary casts, see GH #1064
if np.issubdtype(data.dtype, np.float64):
encoding.pop("dtype", None)

return Variable(dims, data, attrs, encoding, fastpath=True)
else:
return variable
Expand Down

0 comments on commit 37db2b0

Please sign in to comment.