Skip to content

Commit 5be20fa

Browse files
committed
preserve boolean-dtype within encoding, adapt test
1 parent 7af95e8 commit 5be20fa

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

xarray/coding/variables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ def encode(self, variable: Variable, name: T_Name = None) -> Variable:
460460
def decode(self, variable: Variable, name: T_Name = None) -> Variable:
461461
if variable.attrs.get("dtype", False) == "bool":
462462
dims, data, attrs, encoding = unpack_for_decoding(variable)
463-
del attrs["dtype"]
463+
# overwrite encoding accordingly and remove from attrs
464+
encoding["dtype"] = attrs.pop("dtype")
464465
data = BoolTypeArray(data)
465466
return Variable(dims, data, attrs, encoding, fastpath=True)
466467
else:

xarray/tests/test_backends.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ def test_roundtrip_boolean_dtype(self) -> None:
630630
with self.roundtrip(original) as actual:
631631
assert_identical(original, actual)
632632
assert actual["x"].dtype == "bool"
633+
with self.roundtrip(actual) as actual2:
634+
assert_identical(original, actual2)
635+
assert actual2["x"].dtype == "bool"
633636

634637
def test_orthogonal_indexing(self) -> None:
635638
in_memory = create_test_data()

0 commit comments

Comments
 (0)