Skip to content

Commit 25c579a

Browse files
authored
BUG: IntervalIndex.get_indexer raising for read only array (#53703)
1 parent 317730f commit 25c579a

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ Strings
401401

402402
Interval
403403
^^^^^^^^
404-
-
404+
- :meth:`pd.IntervalIndex.get_indexer` and :meth:`pd.IntervalIndex.get_indexer_nonunique` raising if ``target`` is read-only array (:issue:`53703`)
405405
-
406406

407407
Indexing

pandas/_libs/intervaltree.pxi.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ cdef class IntervalTree(IntervalMixin):
125125
sort_order = self.left_sorter
126126
return is_monotonic(sort_order, False)[0]
127127

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

156-
def get_indexer_non_unique(self, scalar_t[:] target):
156+
def get_indexer_non_unique(self, ndarray[scalar_t, ndim=1] target):
157157
"""Return the positions corresponding to intervals that overlap with
158158
the given array of scalar targets. Non-unique positions are repeated.
159159
"""

pandas/tests/indexes/interval/test_indexing.py

+11
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ def test_get_indexer_interval_index(self, box):
424424
expected = np.array([-1, -1, -1], dtype=np.intp)
425425
tm.assert_numpy_array_equal(actual, expected)
426426

427+
def test_get_indexer_read_only(self):
428+
idx = interval_range(start=0, end=5)
429+
arr = np.array([1, 2])
430+
arr.flags.writeable = False
431+
result = idx.get_indexer(arr)
432+
expected = np.array([0, 1])
433+
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
434+
435+
result = idx.get_indexer_non_unique(arr)[0]
436+
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
437+
427438

428439
class TestSliceLocs:
429440
def test_slice_locs_with_interval(self):

0 commit comments

Comments
 (0)