Skip to content

Commit

Permalink
ERR: Make get_indexer return the correct indexer when Index is numeri…
Browse files Browse the repository at this point in the history
…c and target is boolean (#16877)
  • Loading branch information
andrejonasson committed Aug 27, 2017
1 parent 473a7f3 commit 0aaaad8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ Indexing
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
- 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`)
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
- :func:`get_indexer` now returns the correct indexer when a numeric ``Index`` is passed a boolean target (:issue:`16877`)

I/O
^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,9 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
if tolerance is not None:
tolerance = self._convert_tolerance(tolerance)

if target.is_boolean() and self.is_numeric():
return np.repeat(-1, target.size)

pself, ptarget = self._maybe_promote(target)
if pself is not self or ptarget is not target:
return pself.get_indexer(ptarget, method=method, limit=limit,
Expand Down Expand Up @@ -2612,7 +2615,6 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
'backfill or nearest reindexing')

indexer = self._engine.get_indexer(target._values)

return _ensure_platform_int(indexer)

def _convert_tolerance(self, tolerance):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,18 @@ def test_get_indexer_strings(self):
with pytest.raises(TypeError):
idx.get_indexer(['a', 'b', 'c', 'd'], method='pad', tolerance=2)

def test_get_indexer_boolean_index_boolean_target(self):
boolean_idx = pd.Index([True, False])
actual = boolean_idx.get_indexer([True, True, False])
expected = np.array([0, 0, 1])
tm.assert_numpy_array_equal(actual, expected, check_dtype=False)

def test_get_indexer_numeric_index_boolean_target(self):
numeric_idx = pd.Index(range(4))
actual = numeric_idx.get_indexer([True, False, True])
expected = np.repeat(-1, 3)
tm.assert_numpy_array_equal(actual, expected, check_dtype=False)

def test_get_loc(self):
idx = pd.Index([0, 1, 2])
all_methods = [None, 'pad', 'backfill', 'nearest']
Expand Down

0 comments on commit 0aaaad8

Please sign in to comment.