diff --git a/xarray/core/indexes.py b/xarray/core/indexes.py index 4dc71c40bcd..e5c27411d43 100644 --- a/xarray/core/indexes.py +++ b/xarray/core/indexes.py @@ -139,9 +139,11 @@ def _is_nested_tuple(possible_tuple): def normalize_label(value, dtype=None) -> np.ndarray: if getattr(value, "ndim", 1) <= 1: value = _asarray_tuplesafe(value) - if dtype is not None and dtype.kind == "f": + if dtype is not None and dtype.kind == "f" and value.dtype.kind != "b": # pd.Index built from coordinate with float precision != 64 # see https://github.com/pydata/xarray/pull/3153 for details + # bypass coercing dtype for boolean indexers (ignore index) + # see https://github.com/pydata/xarray/issues/5727 value = np.asarray(value, dtype=dtype) return value diff --git a/xarray/tests/test_indexes.py b/xarray/tests/test_indexes.py index 309bab5f95a..24c000d2dcf 100644 --- a/xarray/tests/test_indexes.py +++ b/xarray/tests/test_indexes.py @@ -88,6 +88,16 @@ def test_query(self) -> None: with pytest.raises(ValueError, match=r"does not have a MultiIndex"): index.query({"x": {"one": 0}}) + def test_query_boolean(self) -> None: + # index should be ignored and indexer dtype should not be coerced + # see https://github.com/pydata/xarray/issues/5727 + index = PandasIndex(pd.Index([0.0, 2.0, 1.0, 3.0]), "x") + actual = index.query({"x": [False, True, False, True]}) + expected_dim_indexers = {"x": [False, True, False, True]} + np.testing.assert_array_equal( + actual.dim_indexers["x"], expected_dim_indexers["x"] + ) + def test_query_datetime(self) -> None: index = PandasIndex( pd.to_datetime(["2000-01-01", "2001-01-01", "2002-01-01"]), "x"