Skip to content

Commit 7e3fb66

Browse files
committed
ERR: Warn when Index is numeric and indexer is boolean (pandas-dev#16877)
1 parent 34c4ffd commit 7e3fb66

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ Indexing
330330
- Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`)
331331
- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`)
332332
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
333+
- Warns when a numeric ``Index``'s ``get_indexer`` is passed a boolean indexer (:issue:`16877`)
333334

334335
I/O
335336
^^^

pandas/core/indexes/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,10 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
25852585
if tolerance is not None:
25862586
tolerance = self._convert_tolerance(tolerance)
25872587

2588+
if target.is_boolean() and self.is_numeric():
2589+
warnings.warn('Index is numeric, indexer is boolean',
2590+
RuntimeWarning, stacklevel=2)
2591+
25882592
pself, ptarget = self._maybe_promote(target)
25892593
if pself is not self or ptarget is not target:
25902594
return pself.get_indexer(ptarget, method=method, limit=limit,

pandas/tests/indexes/test_base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,17 @@ 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_target(self):
1135+
boolean_idx = pd.Index([True, False])
1136+
1137+
actual = boolean_idx.get_indexer([True, True, False])
1138+
expected = np.array([0, 0, 1])
1139+
tm.assert_numpy_array_equal(actual, expected)
1140+
1141+
numeric_idx = pd.Index(range(4))
1142+
with pytest.warns(RuntimeWarning):
1143+
numeric_idx.get_indexer([True])
1144+
11341145
def test_get_loc(self):
11351146
idx = pd.Index([0, 1, 2])
11361147
all_methods = [None, 'pad', 'backfill', 'nearest']

0 commit comments

Comments
 (0)