From f1d2bf76d9d0c51b28b9f584615aebaa5ccdb945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Mon, 18 Sep 2023 15:13:55 +0200 Subject: [PATCH] remove `dtype` from encoding for datetime64/timedelta64 variables to prevent unnecessary casts --- xarray/coding/times.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xarray/coding/times.py b/xarray/coding/times.py index 79efbecfb7c..3f21664b0a2 100644 --- a/xarray/coding/times.py +++ b/xarray/coding/times.py @@ -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 @@ -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