Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve boolean dtype in encoding #7720

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Bug fixes
(:issue:`7420`, :pull:`7441`).
By `Justus Magin <https://github.com/keewis>`_ and
`Spencer Clark <https://github.com/spencerkclark>`_.

- Preserve boolean dtype within encoding (:issue:`7652`, :pull:`7720`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_

Documentation
~~~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion xarray/coding/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down