Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 497c60d

Browse files
BUG: .get_indexer_non_unique() must return an array of ints (pandas-dev#44084)
GH#44084 boils down to the following. According to the docs `.get_indexer_non_unique()` is supposed to return "integers from 0 to n - 1 indicating that the index at these positions matches the corresponding target values". However, for an index that is non unique and non monotonic it returns a boolean mask. That is because it uses `.get_loc()` which for non unique, non monotonic indexes returns a boolean mask. This patch catches that case and converts the boolean mask from `.get_loc()` into the corresponding array of integers if the index is not unique and not monotonic.
1 parent 01b86ed commit 497c60d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

pandas/core/indexes/interval.py

+2
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ def _get_indexer_pointwise(
727727
if isinstance(locs, slice):
728728
# Only needed for get_indexer_non_unique
729729
locs = np.arange(locs.start, locs.stop, locs.step, dtype="intp")
730+
elif not self.is_unique and not self.is_monotonic:
731+
locs = np.where(locs)[0]
730732
locs = np.array(locs, ndmin=1)
731733
except KeyError:
732734
missing.append(i)

pandas/tests/indexes/interval/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ def test_get_indexer_with_nans(self):
373373
expected = np.array([0, 1], dtype=np.intp)
374374
tm.assert_numpy_array_equal(result, expected)
375375

376+
def test_get_index_non_unique_non_monotonic(self):
377+
# GH#44084
378+
index = IntervalIndex.from_tuples(
379+
[(0.0, 1.0), (1.0, 2.0), (0.0, 1.0), (1.0, 2.0)]
380+
)
381+
382+
result, _ = index.get_indexer_non_unique([Interval(1.0, 2.0)])
383+
expected = np.array([1, 3])
384+
tm.assert_numpy_array_equal(result, expected)
385+
376386

377387
class TestSliceLocs:
378388
def test_slice_locs_with_interval(self):

0 commit comments

Comments
 (0)