Skip to content

BUG: .get_indexer_non_unique() must return an array of ints (#44084) #44404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ Strings

Interval
^^^^^^^^
-
- 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`)
-

Indexing
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ def _get_indexer_pointwise(
if isinstance(locs, slice):
# Only needed for get_indexer_non_unique
locs = np.arange(locs.start, locs.stop, locs.step, dtype="intp")
elif not self.is_unique and not self.is_monotonic:
locs = np.where(locs)[0]
locs = np.array(locs, ndmin=1)
except KeyError:
missing.append(i)
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/indexes/interval/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from pandas import (
NA,
CategoricalIndex,
Index,
Interval,
IntervalIndex,
MultiIndex,
NaT,
Timedelta,
date_range,
Expand Down Expand Up @@ -373,6 +375,31 @@ def test_get_indexer_with_nans(self):
expected = np.array([0, 1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

def test_get_index_non_unique_non_monotonic(self):
# GH#44084 (root cause)
index = IntervalIndex.from_tuples(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the MultiIndex cases

[(0.0, 1.0), (1.0, 2.0), (0.0, 1.0), (1.0, 2.0)]
)

result, _ = index.get_indexer_non_unique([Interval(1.0, 2.0)])
expected = np.array([1, 3], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

def test_get_indexer_multiindex_with_intervals(self):
# GH#44084 (MultiIndex case as reported)
interval_index = IntervalIndex.from_tuples(
[(2.0, 3.0), (0.0, 1.0), (1.0, 2.0)], name="interval"
)
foo_index = Index([1, 2, 3], name="foo")

multi_index = MultiIndex.from_product([foo_index, interval_index])

result = multi_index.get_level_values("interval").get_indexer_for(
[Interval(0.0, 1.0)]
)
expected = np.array([1, 4, 7], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)


class TestSliceLocs:
def test_slice_locs_with_interval(self):
Expand Down