From c49f39cc3640cfbc09769b1df7cc942a1a15916b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 12 Jul 2019 20:45:24 -0700 Subject: [PATCH 1/3] BUG: Fix take with read-only indexer, closes #17192 --- pandas/_libs/algos_take_helper.pxi.in | 8 ++++---- pandas/tests/indexing/test_indexing.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/algos_take_helper.pxi.in b/pandas/_libs/algos_take_helper.pxi.in index 2fea8b17fd9d7..3a3adc71875ed 100644 --- a/pandas/_libs/algos_take_helper.pxi.in +++ b/pandas/_libs/algos_take_helper.pxi.in @@ -148,7 +148,7 @@ def get_dispatch(dtypes): @cython.wraparound(False) @cython.boundscheck(False) cdef inline take_1d_{{name}}_{{dest}}_memview({{c_type_in}}[:] values, - int64_t[:] indexer, + const int64_t[:] indexer, {{c_type_out}}[:] out, fill_value=np.nan): @@ -159,7 +159,7 @@ cdef inline take_1d_{{name}}_{{dest}}_memview({{c_type_in}}[:] values, @cython.wraparound(False) @cython.boundscheck(False) def take_1d_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=1] values, - int64_t[:] indexer, + const int64_t[:] indexer, {{c_type_out}}[:] out, fill_value=np.nan): @@ -178,7 +178,7 @@ def take_1d_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=1] values, @cython.wraparound(False) @cython.boundscheck(False) cdef inline take_2d_axis0_{{name}}_{{dest}}_memview({{c_type_in}}[:, :] values, - int64_t[:] indexer, + const int64_t[:] indexer, {{c_type_out}}[:, :] out, fill_value=np.nan): {{inner_take_2d_axis0}} @@ -205,7 +205,7 @@ def take_2d_axis0_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=2] values, @cython.wraparound(False) @cython.boundscheck(False) cdef inline take_2d_axis1_{{name}}_{{dest}}_memview({{c_type_in}}[:, :] values, - int64_t[:] indexer, + const int64_t[:] indexer, {{c_type_out}}[:, :] out, fill_value=np.nan): {{inner_take_2d_axis1}} diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 77052de5e80e6..07258b567fd0f 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -1244,3 +1244,18 @@ def test_ndframe_indexing_raises(idxr, error, error_message): frame = NDFrame(np.random.randint(5, size=(2, 2, 2))) with pytest.raises(error, match=error_message): idxr(frame)[0] + + +def test_readonly_indices(): + # GH#17192 iloc with read-only array raising TypeError + df = pd.DataFrame({'data': np.ones(100, dtype='float64')}) + indices = np.array([1, 3, 6]) + indices.flags.writeable = False + + result = df.iloc[indices] + expected = df.loc[[1, 3, 6]] + tm.assert_frame_equal(result, expected) + + result = df['data'].iloc[indices] + expected = df['data'].loc[[1, 3, 6]] + tm.assert_series_equal(result, expected) From 15ce4763223dc1693f618fa18ee3b26b326247e3 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 12 Jul 2019 20:46:55 -0700 Subject: [PATCH 2/3] blackify --- pandas/tests/indexing/test_indexing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 07258b567fd0f..c29b0d644601a 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -1248,7 +1248,7 @@ def test_ndframe_indexing_raises(idxr, error, error_message): def test_readonly_indices(): # GH#17192 iloc with read-only array raising TypeError - df = pd.DataFrame({'data': np.ones(100, dtype='float64')}) + df = pd.DataFrame({"data": np.ones(100, dtype="float64")}) indices = np.array([1, 3, 6]) indices.flags.writeable = False @@ -1256,6 +1256,6 @@ def test_readonly_indices(): expected = df.loc[[1, 3, 6]] tm.assert_frame_equal(result, expected) - result = df['data'].iloc[indices] - expected = df['data'].loc[[1, 3, 6]] + result = df["data"].iloc[indices] + expected = df["data"].loc[[1, 3, 6]] tm.assert_series_equal(result, expected) From 555c3157b6823d3879edc54a6560e34bd1596b49 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 12 Jul 2019 20:48:03 -0700 Subject: [PATCH 3/3] whatsnew --- doc/source/whatsnew/v0.25.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index eeaafd7ad7d51..7397ae8fda80c 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -1054,6 +1054,7 @@ Indexing - 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`) - Bug in setting a new value in a :class:`Series` with a :class:`Timedelta` object incorrectly casting the value to an integer (:issue:`22717`) - Bug in :class:`Series` setting a new key (``__setitem__``) with a timezone-aware datetime incorrectly raising ``ValueError`` (:issue:`12862`) +- Bug in :meth:`DataFrame.iloc` when indexing with a read-only indexer (:issue:`17192`) - Missing