Skip to content

Commit 6ba6fae

Browse files
LucasG0jorisvandenbossche
authored andcommitted
apacheGH-38768: [Python] Empty slicing an array backwards beyond the start is now empty (apache#40682)
### What changes are included in this PR? `_normalize_slice` now relies on `slice.indices` (https://docs.python.org/3/reference/datamodel.html#slice.indices). ### Are these changes tested? Yes. ### Are there any user-facing changes? Fixing wrong data returned in an edge case. * GitHub Issue: apache#40642 * GitHub Issue: apache#38768 Lead-authored-by: LucasG0 <guillermou.lucas@gmail.com> Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com> Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
1 parent 852308c commit 6ba6fae

File tree

2 files changed

+2
-28
lines changed

2 files changed

+2
-28
lines changed

python/pyarrow/array.pxi

+1-28
Original file line numberDiff line numberDiff line change
@@ -561,34 +561,7 @@ def _normalize_slice(object arrow_obj, slice key):
561561
Py_ssize_t start, stop, step
562562
Py_ssize_t n = len(arrow_obj)
563563

564-
step = key.step or 1
565-
566-
if key.start is None:
567-
if step < 0:
568-
start = n - 1
569-
else:
570-
start = 0
571-
elif key.start < 0:
572-
start = key.start + n
573-
if start < 0:
574-
start = 0
575-
elif key.start >= n:
576-
start = n
577-
else:
578-
start = key.start
579-
580-
if step < 0 and (key.stop is None or key.stop < -n):
581-
stop = -1
582-
elif key.stop is None:
583-
stop = n
584-
elif key.stop < 0:
585-
stop = key.stop + n
586-
if stop < 0: # step > 0 in this case.
587-
stop = 0
588-
elif key.stop >= n:
589-
stop = n
590-
else:
591-
stop = key.stop
564+
start, stop, step = key.indices(n)
592565

593566
if step != 1:
594567
indices = np.arange(start, stop, step)

python/pyarrow/tests/test_array.py

+1
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ def test_array_slice_negative_step():
486486
slice(None, None, 2),
487487
slice(0, 10, 2),
488488
slice(15, -25, -1), # GH-38768
489+
slice(-22, -22, -1), # GH-40642
489490
]
490491

491492
for case in cases:

0 commit comments

Comments
 (0)