Skip to content

Commit 0f725bf

Browse files
jbrockmendeljreback
authored andcommitted
BUG: Fix take with read-only indexer, closes #17192 (#27375)
1 parent b9d6433 commit 0f725bf

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ Indexing
10541054
- Bug in :class:`CategoricalIndex` and :class:`Categorical` incorrectly raising ``ValueError`` instead of ``TypeError`` when a list is passed using the ``in`` operator (``__contains__``) (:issue:`21729`)
10551055
- Bug in setting a new value in a :class:`Series` with a :class:`Timedelta` object incorrectly casting the value to an integer (:issue:`22717`)
10561056
- Bug in :class:`Series` setting a new key (``__setitem__``) with a timezone-aware datetime incorrectly raising ``ValueError`` (:issue:`12862`)
1057+
- Bug in :meth:`DataFrame.iloc` when indexing with a read-only indexer (:issue:`17192`)
10571058
-
10581059
10591060
Missing

pandas/_libs/algos_take_helper.pxi.in

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def get_dispatch(dtypes):
148148
@cython.wraparound(False)
149149
@cython.boundscheck(False)
150150
cdef inline take_1d_{{name}}_{{dest}}_memview({{c_type_in}}[:] values,
151-
int64_t[:] indexer,
151+
const int64_t[:] indexer,
152152
{{c_type_out}}[:] out,
153153
fill_value=np.nan):
154154

@@ -159,7 +159,7 @@ cdef inline take_1d_{{name}}_{{dest}}_memview({{c_type_in}}[:] values,
159159
@cython.wraparound(False)
160160
@cython.boundscheck(False)
161161
def take_1d_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=1] values,
162-
int64_t[:] indexer,
162+
const int64_t[:] indexer,
163163
{{c_type_out}}[:] out,
164164
fill_value=np.nan):
165165

@@ -178,7 +178,7 @@ def take_1d_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=1] values,
178178
@cython.wraparound(False)
179179
@cython.boundscheck(False)
180180
cdef inline take_2d_axis0_{{name}}_{{dest}}_memview({{c_type_in}}[:, :] values,
181-
int64_t[:] indexer,
181+
const int64_t[:] indexer,
182182
{{c_type_out}}[:, :] out,
183183
fill_value=np.nan):
184184
{{inner_take_2d_axis0}}
@@ -205,7 +205,7 @@ def take_2d_axis0_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=2] values,
205205
@cython.wraparound(False)
206206
@cython.boundscheck(False)
207207
cdef inline take_2d_axis1_{{name}}_{{dest}}_memview({{c_type_in}}[:, :] values,
208-
int64_t[:] indexer,
208+
const int64_t[:] indexer,
209209
{{c_type_out}}[:, :] out,
210210
fill_value=np.nan):
211211
{{inner_take_2d_axis1}}

pandas/tests/indexing/test_indexing.py

+15
Original file line numberDiff line numberDiff line change
@@ -1244,3 +1244,18 @@ def test_ndframe_indexing_raises(idxr, error, error_message):
12441244
frame = NDFrame(np.random.randint(5, size=(2, 2, 2)))
12451245
with pytest.raises(error, match=error_message):
12461246
idxr(frame)[0]
1247+
1248+
1249+
def test_readonly_indices():
1250+
# GH#17192 iloc with read-only array raising TypeError
1251+
df = pd.DataFrame({"data": np.ones(100, dtype="float64")})
1252+
indices = np.array([1, 3, 6])
1253+
indices.flags.writeable = False
1254+
1255+
result = df.iloc[indices]
1256+
expected = df.loc[[1, 3, 6]]
1257+
tm.assert_frame_equal(result, expected)
1258+
1259+
result = df["data"].iloc[indices]
1260+
expected = df["data"].loc[[1, 3, 6]]
1261+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)