Skip to content

Commit 5f877fa

Browse files
committed
ERR: get_indexer returns the correct indexer when Index is numeric and target is boolean (#16877)
1 parent ad7d6fc commit 5f877fa

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Indexing
360360
- 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`)
361361
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
362362
- Bug in ``CategoricalIndex`` reindexing in which specified indices containing duplicates were not being respected (:issue:`17323`)
363+
- Bug in :func:`DataFrame.drop` caused boolean labels ``False`` and ``True`` to be treated as labels 0 and 1 respectively when dropping indices from a numeric index (:issue:`16877`)
363364

364365
I/O
365366
^^^

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,9 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
25892589
if tolerance is not None:
25902590
tolerance = self._convert_tolerance(tolerance)
25912591

2592+
if target.is_boolean() and self.is_numeric():
2593+
return np.repeat(-1, target.size)
2594+
25922595
pself, ptarget = self._maybe_promote(target)
25932596
if pself is not self or ptarget is not target:
25942597
return pself.get_indexer(ptarget, method=method, limit=limit,
@@ -2617,7 +2620,6 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
26172620
'backfill or nearest reindexing')
26182621

26192622
indexer = self._engine.get_indexer(target._values)
2620-
26212623
return _ensure_platform_int(indexer)
26222624

26232625
def _convert_tolerance(self, tolerance):

pandas/tests/indexes/test_base.py

+7
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,13 @@ def test_get_indexer_strings(self):
11401140
with pytest.raises(TypeError):
11411141
idx.get_indexer(['a', 'b', 'c', 'd'], method='pad', tolerance=2)
11421142

1143+
def test_get_indexer_numeric_index_boolean_target(self):
1144+
# GH 16877
1145+
numeric_idx = pd.Index(range(4))
1146+
result = numeric_idx.get_indexer([True, False, True])
1147+
expected = np.repeat(-1, 3)
1148+
tm.assert_numpy_array_equal(result, expected)
1149+
11431150
def test_get_loc(self):
11441151
idx = pd.Index([0, 1, 2])
11451152
all_methods = [None, 'pad', 'backfill', 'nearest']

pandas/tests/series/test_indexing.py

+5
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,11 @@ def test_drop(self):
17831783
expected = Series([3], index=[False])
17841784
assert_series_equal(result, expected)
17851785

1786+
# GH 16877
1787+
s = Series([2, 3], index=[0, 1])
1788+
with tm.assert_raises_regex(ValueError, 'not contained in axis'):
1789+
s.drop([False, True])
1790+
17861791
def test_align(self):
17871792
def _check_align(a, b, how='left', fill=None):
17881793
aa, ab = a.align(b, join=how, fill_value=fill)

0 commit comments

Comments
 (0)