Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,16 @@ def _validate_key(self, key, axis: AxisInt) -> None:
if not is_numeric_dtype(arr.dtype):
raise IndexError(f".iloc requires numeric indexers, got {arr}")

# check that the key does not exceed the maximum size of the index
if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
raise IndexError("positional indexers are out-of-bounds")
if len(arr):
# convert to numpy array for min/max with ExtensionArrays
if hasattr(arr, "to_numpy"):
np_arr = arr.to_numpy()
else:
np_arr = np.asarray(arr)
Copy link
Member

Choose a reason for hiding this comment

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

Check if arr is an ExtensionArray using if isinstance(arr.dtype, ExtensionDtype) and use arr._reduce("max") instead


# check that the key does not exceed the maximum size
if np.max(np_arr) >= len_axis or np.min(np_arr) < -len_axis:
raise IndexError("positional indexers are out-of-bounds")
else:
raise ValueError(f"Can only index by location with a [{self._valid_types}]")

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,3 +1478,14 @@ def test_iloc_nullable_int64_size_1_nan(self):
result = DataFrame({"a": ["test"], "b": [np.nan]})
with pytest.raises(TypeError, match="Invalid value"):
result.loc[:, "b"] = result.loc[:, "b"].astype("Int64")

def test_iloc_arrow_extension_array(self):
# GH#61311
pytest.importorskip("pyarrow")

df = DataFrame({"a": [1, 2], "c": [0, 2], "d": ["c", "a"]})

df_arrow = df.convert_dtypes(dtype_backend="pyarrow")
result = df_arrow.iloc[:, df_arrow["c"]]
expected = df_arrow.iloc[:, [0, 2]]
Copy link
Member

Choose a reason for hiding this comment

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

Construct this using DataFrame(...) instead

tm.assert_frame_equal(result, expected)
Loading