GH-51145: [Python] Accept Arrow integer scalars in slice methods - #51160
Open
YusefSyed wants to merge 2 commits into
Open
GH-51145: [Python] Accept Arrow integer scalars in slice methods#51160YusefSyed wants to merge 2 commits into
YusefSyed wants to merge 2 commits into
Conversation
AlenkaF
reviewed
Sep 10, 2026
AlenkaF
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! I was thinking of suggesting the use of Py_ssize_t-typed Cython parameter but operator.index() does solve everything: accepts Python ints and Arrow Scalars if they are integer type. cc @raulcd what do you think?
In any case, we need to update docstrings for offset and length as they do not need to be int but can also be Arrow Scalars.
Update offset and length documentation for all four slice methods. Arrow scalars must be non-null integers. Prepared and validated with OpenAI Codex assistance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
Array.slice,ChunkedArray.slice,RecordBatch.slice, andTable.sliceaccept Python and NumPy integer-like values but reject Arrow integer scalars,
even though those scalars implement Python's integer-index protocol. This makes
values produced by Arrow APIs unnecessarily unusable as slice offsets and
lengths.
What changes are included in this PR?
Normalize non-
Noneoffsets and lengths withoperator.index()at the fourpublic Python wrapper boundaries before the existing validation and C++ calls.
Add regression coverage for all signed and unsigned Arrow integer scalar types
across the four wrappers, plus NumPy compatibility, invalid and null scalars,
negative values, offset clamping, and int64 overflow behavior.
Are these changes tested?
Yes. Current Arrow C++ and editable PyArrow were built from source in an
isolated Python 3.13 environment. The focused new tests and existing slice
controls passed: 48 passed.
Cython translation, Python test-file compilation, Flake8, and
git diff --checkalso passed.Are there any user-facing changes?
Yes. The four Python
.slice()methods now accept non-null Arrow integerscalars for offsets and lengths. Existing Python/NumPy integer behavior and
error behavior for unsupported values are preserved.
AI assistance
AI assisted analysis, implementation, test drafting, build work, and review.
The account holder reviewed and approved the final four-file diff. The account
holder did not personally run the commands reported above.
.slicemethods should accept Arrow scalar types #51145