Skip to content

BUG: Add min/max methods to ArrowExtensionArray GH#61311 #61924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,9 +1609,18 @@ 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):
# handle ExtensionArray using _reduce method else use numpy
if isinstance(arr.dtype, ExtensionDtype):
arr_max = arr._reduce("max")
arr_min = arr._reduce("min")
else:
arr_max = np.max(arr)
arr_min = np.min(arr)

# check that the key does not exceed the maximum size
if arr_max >= len_axis or arr_min < -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 = DataFrame(
{"a": [1, 2], "c": [0, 2], "d": ["c", "a"]}
).convert_dtypes(dtype_backend="pyarrow")
expected = df.iloc[:, df["c"]]
result = df_arrow.iloc[:, df_arrow["c"]]
tm.assert_frame_equal(result, expected, check_dtype=False)
Loading