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 18, 2023
1 parent b08a9d8 commit f1d2bf7
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions xarray/coding/times.py
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)

# remove dtype from encoding to prevent unnecessary casts
# see GH #1064
if "dtype" in encoding:
encoding.pop("dtype")

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)

# remove dtype from encoding to prevent unnecessary casts
# see GH #1064
if "dtype" in encoding:
encoding.pop("dtype")

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

0 comments on commit f1d2bf7

Please sign in to comment.