Skip to content

Commit

Permalink
BUG: IntervalIndex.get_indexer raising for read only array (#53703)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Jun 19, 2023
1 parent 317730f commit 25c579a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Strings

Interval
^^^^^^^^
-
- :meth:`pd.IntervalIndex.get_indexer` and :meth:`pd.IntervalIndex.get_indexer_nonunique` raising if ``target`` is read-only array (:issue:`53703`)
-

Indexing
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/intervaltree.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ cdef class IntervalTree(IntervalMixin):
sort_order = self.left_sorter
return is_monotonic(sort_order, False)[0]

def get_indexer(self, scalar_t[:] target) -> np.ndarray:
def get_indexer(self, ndarray[scalar_t, ndim=1] target) -> np.ndarray:
"""Return the positions corresponding to unique intervals that overlap
with the given array of scalar targets.
"""
Expand Down Expand Up @@ -153,7 +153,7 @@ cdef class IntervalTree(IntervalMixin):
old_len = result.data.n
return result.to_array().astype('intp')

def get_indexer_non_unique(self, scalar_t[:] target):
def get_indexer_non_unique(self, ndarray[scalar_t, ndim=1] target):
"""Return the positions corresponding to intervals that overlap with
the given array of scalar targets. Non-unique positions are repeated.
"""
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/interval/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ def test_get_indexer_interval_index(self, box):
expected = np.array([-1, -1, -1], dtype=np.intp)
tm.assert_numpy_array_equal(actual, expected)

def test_get_indexer_read_only(self):
idx = interval_range(start=0, end=5)
arr = np.array([1, 2])
arr.flags.writeable = False
result = idx.get_indexer(arr)
expected = np.array([0, 1])
tm.assert_numpy_array_equal(result, expected, check_dtype=False)

result = idx.get_indexer_non_unique(arr)[0]
tm.assert_numpy_array_equal(result, expected, check_dtype=False)


class TestSliceLocs:
def test_slice_locs_with_interval(self):
Expand Down

0 comments on commit 25c579a

Please sign in to comment.