Skip to content

Commit

Permalink
do not coerce bool indexer as float coord dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy committed Sep 27, 2021
1 parent 6c6f09e commit 6437e13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6437e13

Please sign in to comment.