Skip to content

Commit 085b9a0

Browse files
committed
ERR: Make get_indexer return the correct indexer when Index is numeric and target is boolean (#16877)
1 parent 473a7f3 commit 085b9a0

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ Indexing
358358
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
359359
- Bug in ``.iloc`` when used with inplace addition or assignment and an int indexer on a ``MultiIndex`` causing the wrong indexes to be read from and written to (:issue:`17148`)
360360
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
361+
- When a numeric ``Index``'s :func:`get_indexer` is passed a boolean target it now returns the correct indexer (:issue:`16877`)
361362

362363
I/O
363364
^^^

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,9 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
25842584
if tolerance is not None:
25852585
tolerance = self._convert_tolerance(tolerance)
25862586

2587+
if target.is_boolean() and self.is_numeric():
2588+
return np.repeat(-1, target.size)
2589+
25872590
pself, ptarget = self._maybe_promote(target)
25882591
if pself is not self or ptarget is not target:
25892592
return pself.get_indexer(ptarget, method=method, limit=limit,
@@ -2612,7 +2615,6 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
26122615
'backfill or nearest reindexing')
26132616

26142617
indexer = self._engine.get_indexer(target._values)
2615-
26162618
return _ensure_platform_int(indexer)
26172619

26182620
def _convert_tolerance(self, tolerance):

pandas/tests/indexes/test_base.py

+12
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,18 @@ def test_get_indexer_strings(self):
11311131
with pytest.raises(TypeError):
11321132
idx.get_indexer(['a', 'b', 'c', 'd'], method='pad', tolerance=2)
11331133

1134+
def test_get_indexer_boolean_index_boolean_target(self):
1135+
boolean_idx = pd.Index([True, False])
1136+
actual = boolean_idx.get_indexer([True, True, False])
1137+
expected = np.array([0, 0, 1])
1138+
tm.assert_numpy_array_equal(actual, expected, check_dtype=False)
1139+
1140+
def test_get_indexer_numeric_index_boolean_target(self):
1141+
numeric_idx = pd.Index(range(4))
1142+
actual = numeric_idx.get_indexer([True, False, True])
1143+
expected = np.repeat(-1, 3)
1144+
tm.assert_numpy_array_equal(actual, expected, check_dtype=False)
1145+
11341146
def test_get_loc(self):
11351147
idx = pd.Index([0, 1, 2])
11361148
all_methods = [None, 'pad', 'backfill', 'nearest']

0 commit comments

Comments
 (0)