Skip to content

Commit be85d35

Browse files
committed
Fix _validate_key to handle ExtensionArray correctly GH#61311
1 parent 126d759 commit be85d35

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

pandas/core/indexing.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,9 +1610,16 @@ def _validate_key(self, key, axis: AxisInt) -> None:
16101610
if not is_numeric_dtype(arr.dtype):
16111611
raise IndexError(f".iloc requires numeric indexers, got {arr}")
16121612

1613-
# check that the key does not exceed the maximum size of the index
1614-
if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
1615-
raise IndexError("positional indexers are out-of-bounds")
1613+
if len(arr):
1614+
# convert to numpy array to ensure min/max work with ExtensionArrays
1615+
if hasattr(arr, 'to_numpy'):
1616+
np_arr = arr.to_numpy()
1617+
else:
1618+
np_arr = np.asarray(arr)
1619+
1620+
# check that the key does not exceed the maximum size of the index
1621+
if np.max(np_arr) >= len_axis or np.min(np_arr) < -len_axis:
1622+
raise IndexError("positional indexers are out-of-bounds")
16161623
else:
16171624
raise ValueError(f"Can only index by location with a [{self._valid_types}]")
16181625

pandas/tests/indexing/test_iloc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,3 +1482,18 @@ def test_iloc_nullable_int64_size_1_nan(self):
14821482
result = DataFrame({"a": ["test"], "b": [np.nan]})
14831483
with pytest.raises(TypeError, match="Invalid value"):
14841484
result.loc[:, "b"] = result.loc[:, "b"].astype("Int64")
1485+
1486+
def test_iloc_arrow_extension_array(self):
1487+
# GH#61311
1488+
pytest.importorskip("pyarrow")
1489+
1490+
df = DataFrame({
1491+
"a": [1, 2],
1492+
"c": [0, 2],
1493+
"d": ["c", "a"]
1494+
})
1495+
1496+
df_arrow = df.convert_dtypes(dtype_backend="pyarrow")
1497+
result = df_arrow.iloc[:, df_arrow["c"]]
1498+
expected = df_arrow.iloc[:, [0, 2]]
1499+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)