Skip to content

BUG: get_indexer_non_unique not excluding boolean like get_indexer #38052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down