Skip to content
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

GH-40642: [Python] Empty slicing an array backwards beyond the start is now empty #40682

Merged
Merged
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
29 changes: 1 addition & 28 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -561,34 +561,7 @@ def _normalize_slice(object arrow_obj, slice key):
Py_ssize_t start, stop, step
Py_ssize_t n = len(arrow_obj)

step = key.step or 1

if key.start is None:
if step < 0:
start = n - 1
else:
start = 0
elif key.start < 0:
start = key.start + n
if start < 0:
start = 0
elif key.start >= n:
start = n
else:
start = key.start

if step < 0 and (key.stop is None or key.stop < -n):
stop = -1
elif key.stop is None:
stop = n
elif key.stop < 0:
stop = key.stop + n
if stop < 0: # step > 0 in this case.
stop = 0
elif key.stop >= n:
stop = n
else:
stop = key.stop
start, stop, step = key.indices(n)

if step != 1:
indices = np.arange(start, stop, step)
Expand Down
1 change: 1 addition & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def test_array_slice_negative_step():
slice(None, None, 2),
slice(0, 10, 2),
slice(15, -25, -1), # GH-38768
slice(-22, -22, -1), # GH-40642
]

for case in cases:
Expand Down
Loading