Skip to content

Commit 618de88

Browse files
skonda29mroeschke
andauthored
BUG: Add min/max methods to ArrowExtensionArray GH#61311 (#61924)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 116d3e9 commit 618de88

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

pandas/core/indexing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,9 +1626,17 @@ def _validate_key(self, key, axis: AxisInt) -> None:
16261626
if not is_numeric_dtype(arr.dtype):
16271627
raise IndexError(f".iloc requires numeric indexers, got {arr}")
16281628

1629-
# check that the key does not exceed the maximum size of the index
1630-
if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
1631-
raise IndexError("positional indexers are out-of-bounds")
1629+
if len(arr):
1630+
if isinstance(arr.dtype, ExtensionDtype):
1631+
arr_max = arr._reduce("max")
1632+
arr_min = arr._reduce("min")
1633+
else:
1634+
arr_max = np.max(arr)
1635+
arr_min = np.min(arr)
1636+
1637+
# check that the key does not exceed the maximum size
1638+
if arr_max >= len_axis or arr_min < -len_axis:
1639+
raise IndexError("positional indexers are out-of-bounds")
16321640
else:
16331641
raise ValueError(f"Can only index by ___location with a [{self._valid_types}]")
16341642

pandas/tests/indexing/test_iloc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,3 +1482,14 @@ 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+
df = DataFrame({"a": [1, 2], "c": [0, 2], "d": ["c", "a"]})
1490+
df_arrow = DataFrame(
1491+
{"a": [1, 2], "c": [0, 2], "d": ["c", "a"]}
1492+
).convert_dtypes(dtype_backend="pyarrow")
1493+
expected = df.iloc[:, df["c"]]
1494+
result = df_arrow.iloc[:, df_arrow["c"]]
1495+
tm.assert_frame_equal(result, expected, check_dtype=False)

0 commit comments

Comments
 (0)