This is an automated email from the ASF dual-hosted git repository.

raulcd pushed a commit to branch maint-16.0.0
in repository https://gitbox.apache.org/repos/asf/arrow.git

commit 1558d3f32060570a61a5ef3f8e362cafc1ae5201
Author: LucasG0 <[email protected]>
AuthorDate: Tue Apr 9 17:05:03 2024 +0200

    GH-38768: [Python] Empty slicing an array backwards beyond the start is now 
empty (#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: #40642
    * GitHub Issue: #38768
    
    Lead-authored-by: LucasG0 <[email protected]>
    Co-authored-by: Joris Van den Bossche <[email protected]>
    Signed-off-by: Joris Van den Bossche <[email protected]>
---
 python/pyarrow/array.pxi           | 29 +----------------------------
 python/pyarrow/tests/test_array.py |  1 +
 2 files changed, 2 insertions(+), 28 deletions(-)

diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index 59d2e91ef6..45fd29ad3b 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -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)
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index 60794ebef3..472a6c5dce 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -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:

Reply via email to