Skip to content

Commit 67a8701

Browse files
committed
Fix transpose of BoolTypeArray, NativeEndiannessArray
Closes #10536
1 parent 699d895 commit 67a8701

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Bug fixes
2828
- Fix Pydap Datatree backend testing. Testing now compares elements of (unordered) two sets (before, lists) (:pull:`10525`).
2929
By `Miguel Jimenez-Urias <https://github.com/Mikejmnez>`_.
3030

31+
- Fix transpose of boolean arrays read from disk. (:issue:`10536`)
32+
By `Deepak Cherian <https://github.com/dcherian>`_.
33+
3134

3235
Documentation
3336
~~~~~~~~~~~~~

xarray/coding/variables.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def __getitem__(self, key) -> Self:
7070
def get_duck_array(self):
7171
return duck_array_ops.astype(self.array.get_duck_array(), dtype=self.dtype)
7272

73+
def transpose(self, order):
74+
return type(self)(self.array.transpose(order))
75+
7376

7477
class BoolTypeArray(indexing.ExplicitlyIndexedNDArrayMixin):
7578
"""Decode arrays on the fly from integer to boolean datatype
@@ -111,6 +114,9 @@ def __getitem__(self, key) -> Self:
111114
def get_duck_array(self):
112115
return duck_array_ops.astype(self.array.get_duck_array(), dtype=self.dtype)
113116

117+
def transpose(self, order):
118+
return type(self)(self.array.transpose(order))
119+
114120

115121
def _apply_mask(
116122
data: np.ndarray,

xarray/tests/test_backends.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,15 @@ def create_encoded_unsigned_false_masked_scaled_data(dtype: np.dtype) -> Dataset
308308

309309
def create_boolean_data() -> Dataset:
310310
attributes = {"units": "-"}
311-
return Dataset({"x": ("t", [True, False, False, True], attributes)})
311+
return Dataset(
312+
{
313+
"x": (
314+
("t", "x"),
315+
[[False, True, False, True], [True, False, False, True]],
316+
attributes,
317+
)
318+
}
319+
)
312320

313321

314322
class TestCommon:
@@ -726,6 +734,9 @@ def test_roundtrip_boolean_dtype(self) -> None:
726734
with self.roundtrip(actual) as actual2:
727735
assert_identical(original, actual2)
728736
assert actual2["x"].dtype == "bool"
737+
with self.roundtrip(actual) as actual3:
738+
# GH10536
739+
assert_identical(original.transpose(), actual3.transpose())
729740

730741
def test_orthogonal_indexing(self) -> None:
731742
in_memory = create_test_data()

xarray/tests/test_conventions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def test_booltype_array(self) -> None:
3737
assert bx.dtype == bool
3838
assert_array_equal(bx, np.array([True, False, True, True, False], dtype=bool))
3939

40+
x = np.array([[1, 0, 1], [0, 1, 0]], dtype="i1")
41+
bx = coding.variables.BoolTypeArray(x)
42+
assert_array_equal(bx.transpose((1, 0)), x.transpose((1, 0)))
43+
4044

4145
class TestNativeEndiannessArray:
4246
def test(self) -> None:
@@ -47,6 +51,11 @@ def test(self) -> None:
4751
assert a.dtype == expected[:].dtype
4852
assert_array_equal(a, expected)
4953

54+
x = np.arange(6, dtype=">i8").reshape((2, 3))
55+
a = coding.variables.NativeEndiannessArray(x)
56+
expected = np.arange(6, dtype="int64").reshape((2, 3))
57+
assert_array_equal(a.transpose((1, 0)), expected.transpose((1, 0)))
58+
5059

5160
def test_decode_cf_with_conflicting_fill_missing_value() -> None:
5261
expected = Variable(["t"], [np.nan, np.nan, 2], {"units": "foobar"})

0 commit comments

Comments
 (0)