Skip to content

Commit d3039ad

Browse files
committed
use _reduce method for ExtensionArrays
1 parent f87de21 commit d3039ad

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

pandas/core/indexing.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,14 +1610,15 @@ def _validate_key(self, key, axis: AxisInt) -> None:
16101610
raise IndexError(f".iloc requires numeric indexers, got {arr}")
16111611

16121612
if len(arr):
1613-
# convert to numpy array for min/max with ExtensionArrays
1614-
if hasattr(arr, "to_numpy"):
1615-
np_arr = arr.to_numpy()
1613+
# handle ExtensionArray safely using _reduce method else use numpy
1614+
if isinstance(arr.dtype, ExtensionDtype):
1615+
arr_max = arr._reduce("max")
1616+
arr_min = arr._reduce("min")
16161617
else:
1617-
np_arr = np.asarray(arr)
1618+
arr_max = np.max(arr)
1619+
arr_min = np.min(arr)
16181620

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

pandas/tests/indexing/test_iloc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,10 +1482,10 @@ def test_iloc_nullable_int64_size_1_nan(self):
14821482
def test_iloc_arrow_extension_array(self):
14831483
# GH#61311
14841484
pytest.importorskip("pyarrow")
1485-
14861485
df = DataFrame({"a": [1, 2], "c": [0, 2], "d": ["c", "a"]})
1487-
1488-
df_arrow = df.convert_dtypes(dtype_backend="pyarrow")
1486+
df_arrow = DataFrame(
1487+
{"a": [1, 2], "c": [0, 2], "d": ["c", "a"]}
1488+
).convert_dtypes(dtype_backend="pyarrow")
1489+
expected = df.iloc[:, df["c"]]
14891490
result = df_arrow.iloc[:, df_arrow["c"]]
1490-
expected = df_arrow.iloc[:, [0, 2]]
1491-
tm.assert_frame_equal(result, expected)
1491+
tm.assert_frame_equal(result, expected, check_dtype=False)

0 commit comments

Comments
 (0)