Skip to content

Commit 2bbd4d6

Browse files
BUG: .get_indexer_non_unique() must return an array of ints (#44084) (#44404)
1 parent e27db07 commit 2bbd4d6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ Strings
573573

574574
Interval
575575
^^^^^^^^
576-
-
576+
- Bug in :meth:`IntervalIndex.get_indexer_non_unique` returning boolean mask instead of array of integers for a non unique and non monotonic index (:issue:`44084`)
577577
-
578578

579579
Indexing

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

+27
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
from pandas import (
99
NA,
1010
CategoricalIndex,
11+
Index,
1112
Interval,
1213
IntervalIndex,
14+
MultiIndex,
1315
NaT,
1416
Series,
1517
Timedelta,
@@ -374,6 +376,31 @@ def test_get_indexer_with_nans(self):
374376
expected = np.array([0, 1], dtype=np.intp)
375377
tm.assert_numpy_array_equal(result, expected)
376378

379+
def test_get_index_non_unique_non_monotonic(self):
380+
# GH#44084 (root cause)
381+
index = IntervalIndex.from_tuples(
382+
[(0.0, 1.0), (1.0, 2.0), (0.0, 1.0), (1.0, 2.0)]
383+
)
384+
385+
result, _ = index.get_indexer_non_unique([Interval(1.0, 2.0)])
386+
expected = np.array([1, 3], dtype=np.intp)
387+
tm.assert_numpy_array_equal(result, expected)
388+
389+
def test_get_indexer_multiindex_with_intervals(self):
390+
# GH#44084 (MultiIndex case as reported)
391+
interval_index = IntervalIndex.from_tuples(
392+
[(2.0, 3.0), (0.0, 1.0), (1.0, 2.0)], name="interval"
393+
)
394+
foo_index = Index([1, 2, 3], name="foo")
395+
396+
multi_index = MultiIndex.from_product([foo_index, interval_index])
397+
398+
result = multi_index.get_level_values("interval").get_indexer_for(
399+
[Interval(0.0, 1.0)]
400+
)
401+
expected = np.array([1, 4, 7], dtype=np.intp)
402+
tm.assert_numpy_array_equal(result, expected)
403+
377404

378405
class TestSliceLocs:
379406
def test_slice_locs_with_interval(self):

0 commit comments

Comments
 (0)