Skip to content

Commit 6437e13

Browse files
committed
do not coerce bool indexer as float coord dtype
Fixes pydata#5727
1 parent 6c6f09e commit 6437e13

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

xarray/core/indexes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ def _is_nested_tuple(possible_tuple):
139139
def normalize_label(value, dtype=None) -> np.ndarray:
140140
if getattr(value, "ndim", 1) <= 1:
141141
value = _asarray_tuplesafe(value)
142-
if dtype is not None and dtype.kind == "f":
142+
if dtype is not None and dtype.kind == "f" and value.dtype.kind != "b":
143143
# pd.Index built from coordinate with float precision != 64
144144
# see https://github.com/pydata/xarray/pull/3153 for details
145+
# bypass coercing dtype for boolean indexers (ignore index)
146+
# see https://github.com/pydata/xarray/issues/5727
145147
value = np.asarray(value, dtype=dtype)
146148
return value
147149

xarray/tests/test_indexes.py

+10
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def test_query(self) -> None:
8888
with pytest.raises(ValueError, match=r"does not have a MultiIndex"):
8989
index.query({"x": {"one": 0}})
9090

91+
def test_query_boolean(self) -> None:
92+
# index should be ignored and indexer dtype should not be coerced
93+
# see https://github.com/pydata/xarray/issues/5727
94+
index = PandasIndex(pd.Index([0.0, 2.0, 1.0, 3.0]), "x")
95+
actual = index.query({"x": [False, True, False, True]})
96+
expected_dim_indexers = {"x": [False, True, False, True]}
97+
np.testing.assert_array_equal(
98+
actual.dim_indexers["x"], expected_dim_indexers["x"]
99+
)
100+
91101
def test_query_datetime(self) -> None:
92102
index = PandasIndex(
93103
pd.to_datetime(["2000-01-01", "2001-01-01", "2002-01-01"]), "x"

0 commit comments

Comments
 (0)