diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b5900ead246f3..fac4940c0b573 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4896,6 +4896,14 @@ def set_value(self, arr, key, value): @Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs) def get_indexer_non_unique(self, target): target = ensure_index(target) + + if target.is_boolean() and self.is_numeric(): + # Treat boolean labels passed to a numeric index as not found. Without + # this fix False and True would be treated as 0 and 1 respectively. + # (GH #16877) + no_matches = -1 * np.ones(self.shape, dtype=np.intp) + return no_matches, no_matches + pself, ptarget = self._maybe_promote(target) if pself is not self or ptarget is not target: return pself.get_indexer_non_unique(ptarget) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 2e3a70e8c2215..ba2ac5d2e07b2 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1236,14 +1236,25 @@ def test_get_indexer_strings_raises(self): ["a", "b", "c", "d"], method="pad", tolerance=[2, 2, 2, 2] ) - @pytest.mark.parametrize("idx_class", [Int64Index, RangeIndex, Float64Index]) - def test_get_indexer_numeric_index_boolean_target(self, idx_class): + @pytest.mark.parametrize( + "idx_class", [Int64Index, RangeIndex, Float64Index, UInt64Index] + ) + @pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"]) + def test_get_indexer_numeric_index_boolean_target(self, method, idx_class): # GH 16877 numeric_index = idx_class(RangeIndex(4)) - result = numeric_index.get_indexer([True, False, True]) + other = Index([True, False, True]) + + result = getattr(numeric_index, method)(other) expected = np.array([-1, -1, -1], dtype=np.intp) - tm.assert_numpy_array_equal(result, expected) + if method == "get_indexer": + tm.assert_numpy_array_equal(result, expected) + else: + expected = np.array([-1, -1, -1, -1], dtype=np.intp) + + tm.assert_numpy_array_equal(result[0], expected) + tm.assert_numpy_array_equal(result[1], expected) def test_get_indexer_with_NA_values( self, unique_nulls_fixture, unique_nulls_fixture2