Skip to content

Commit 14e0d12

Browse files
authored
BUG: get_indexer_non_unique not excluding boolean like get_indexer (#38052)
1 parent 07ca75a commit 14e0d12

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

pandas/core/indexes/base.py

+8
Original file line numberDiff line numberDiff line change
@@ -4896,6 +4896,14 @@ def set_value(self, arr, key, value):
48964896
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
48974897
def get_indexer_non_unique(self, target):
48984898
target = ensure_index(target)
4899+
4900+
if target.is_boolean() and self.is_numeric():
4901+
# Treat boolean labels passed to a numeric index as not found. Without
4902+
# this fix False and True would be treated as 0 and 1 respectively.
4903+
# (GH #16877)
4904+
no_matches = -1 * np.ones(self.shape, dtype=np.intp)
4905+
return no_matches, no_matches
4906+
48994907
pself, ptarget = self._maybe_promote(target)
49004908
if pself is not self or ptarget is not target:
49014909
return pself.get_indexer_non_unique(ptarget)

pandas/tests/indexes/test_base.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1234,14 +1234,25 @@ def test_get_indexer_strings_raises(self):
12341234
["a", "b", "c", "d"], method="pad", tolerance=[2, 2, 2, 2]
12351235
)
12361236

1237-
@pytest.mark.parametrize("idx_class", [Int64Index, RangeIndex, Float64Index])
1238-
def test_get_indexer_numeric_index_boolean_target(self, idx_class):
1237+
@pytest.mark.parametrize(
1238+
"idx_class", [Int64Index, RangeIndex, Float64Index, UInt64Index]
1239+
)
1240+
@pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"])
1241+
def test_get_indexer_numeric_index_boolean_target(self, method, idx_class):
12391242
# GH 16877
12401243

12411244
numeric_index = idx_class(RangeIndex(4))
1242-
result = numeric_index.get_indexer([True, False, True])
1245+
other = Index([True, False, True])
1246+
1247+
result = getattr(numeric_index, method)(other)
12431248
expected = np.array([-1, -1, -1], dtype=np.intp)
1244-
tm.assert_numpy_array_equal(result, expected)
1249+
if method == "get_indexer":
1250+
tm.assert_numpy_array_equal(result, expected)
1251+
else:
1252+
expected = np.array([-1, -1, -1, -1], dtype=np.intp)
1253+
1254+
tm.assert_numpy_array_equal(result[0], expected)
1255+
tm.assert_numpy_array_equal(result[1], expected)
12451256

12461257
def test_get_indexer_with_NA_values(
12471258
self, unique_nulls_fixture, unique_nulls_fixture2

0 commit comments

Comments
 (0)