diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2e97e61abb1..2df0d58bf6e 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -54,7 +54,8 @@ Bug fixes (:issue:`7420`, :pull:`7441`). By `Justus Magin `_ and `Spencer Clark `_. - +- Preserve boolean dtype within encoding (:issue:`7652`, :pull:`7720`). + By `Kai Mühlbauer `_ Documentation ~~~~~~~~~~~~~ diff --git a/xarray/coding/variables.py b/xarray/coding/variables.py index 6a439418028..5c6e51c2215 100644 --- a/xarray/coding/variables.py +++ b/xarray/coding/variables.py @@ -458,7 +458,9 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable: def decode(self, variable: Variable, name: T_Name = None) -> Variable: if variable.attrs.get("dtype", False) == "bool": dims, data, attrs, encoding = unpack_for_decoding(variable) - del attrs["dtype"] + # overwrite (!) dtype in encoding, and remove from attrs + # needed for correct subsequent encoding + encoding["dtype"] = attrs.pop("dtype") data = BoolTypeArray(data) return Variable(dims, data, attrs, encoding, fastpath=True) else: diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 14b510d4c97..7d58c5bfed2 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -630,6 +630,11 @@ def test_roundtrip_boolean_dtype(self) -> None: with self.roundtrip(original) as actual: assert_identical(original, actual) assert actual["x"].dtype == "bool" + # this checks for preserving dtype during second roundtrip + # see https://github.com/pydata/xarray/issues/7652#issuecomment-1476956975 + with self.roundtrip(actual) as actual2: + assert_identical(original, actual2) + assert actual2["x"].dtype == "bool" def test_orthogonal_indexing(self) -> None: in_memory = create_test_data()