Skip to content

Commit

Permalink
ERR: Raise when Index is numeric and indexer is boolean (pandas-dev#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejonasson committed Aug 26, 2017
1 parent 473a7f3 commit 4a10a11
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
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`)
- Raises when a numeric ``Index``'s :func:`get_indexer` is passed a boolean indexer (:issue:`16877`)

I/O
^^^
Expand Down
3 changes: 3 additions & 0 deletions 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():
raise KeyError('labels %s not contained in axis' % target.values)

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
11 changes: 11 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,17 @@ 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_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)

numeric_idx = pd.Index(range(4))
with tm.assert_raises_regex(KeyError, 'not contained in axis'):
numeric_idx.get_indexer([True])

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

0 comments on commit 4a10a11

Please sign in to comment.