Skip to content

Commit 2c48272

Browse files
Only return -1 in get_indexer for incorrect values
Instead of returning [-1, -1, -1] when the middle value is incorrect type, return [a, -1, b].
1 parent 120e2bc commit 2c48272

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pandas/core/indexes/interval.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,11 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
833833
start, stop = (
834834
self._find_non_overlapping_monotonic_bounds(target)
835835
)
836+
start_plus_one = start + 1
837+
if not ((start_plus_one < stop).any()):
838+
return np.where(start_plus_one == stop, start, -1)
836839
except TypeError:
837-
return np.repeat(np.int(-1), len(target))
838-
839-
start_plus_one = start + 1
840-
if not ((start_plus_one < stop).any()):
841-
return np.where(start_plus_one == stop, start, -1)
840+
pass
842841

843842
if not self.is_unique:
844843
raise ValueError("cannot handle non-unique indices")
@@ -854,8 +853,8 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
854853
try:
855854
vals.append(self.get_loc(i))
856855
except KeyError:
857-
vals.append(np.array([-1]))
858-
indexer = np.concatenate(vals)
856+
vals.append(-1)
857+
indexer = np.array(vals)
859858

860859
return ensure_platform_int(indexer)
861860

pandas/tests/indexes/interval/test_interval.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,14 @@ def test_get_indexer(self):
591591
expected = np.array([-1, 1], dtype='intp')
592592
tm.assert_numpy_array_equal(actual, expected)
593593

594+
actual = self.index.get_indexer(['a', 1])
595+
expected = np.array([-1, 0], dtype='intp')
596+
tm.assert_numpy_array_equal(actual, expected)
597+
598+
actual = self.index.get_indexer(['a', 1, 'b'])
599+
expected = np.array([-1, 0, -1], dtype='intp')
600+
tm.assert_numpy_array_equal(actual, expected)
601+
594602
# To be removed, replaced by test_interval_new.py (see #16316, #16386)
595603
def test_get_indexer_subintervals(self):
596604

0 commit comments

Comments
 (0)