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

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 907a27d  ARROW-2288: [Python] Fix slicing logic
907a27d is described below

commit 907a27d913bbf1d824454b6f2be719f897b61073
Author: Antoine Pitrou <[email protected]>
AuthorDate: Fri Mar 9 15:04:36 2018 -0500

    ARROW-2288: [Python] Fix slicing logic
    
    Author: Antoine Pitrou <[email protected]>
    
    Closes #1723 from pitrou/ARROW-2288-py-slicing-logic and squashes the 
following commits:
    
    0c5461f1 <Antoine Pitrou> ARROW-2288:  Fix slicing logic
---
 python/pyarrow/array.pxi           | 16 +++++++++++++---
 python/pyarrow/tests/test_array.py |  9 +++++----
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index f05806c..321809f 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -205,15 +205,25 @@ def asarray(values, type=None):
 
 
 def _normalize_slice(object arrow_obj, slice key):
-    cdef Py_ssize_t n = len(arrow_obj)
+    cdef:
+        Py_ssize_t start, stop, step
+        Py_ssize_t n = len(arrow_obj)
 
     start = key.start or 0
-    while start < 0:
+    if start < 0:
         start += n
+        if start < 0:
+            start = 0
+    elif start >= n:
+        start = n
 
     stop = key.stop if key.stop is not None else n
-    while stop < 0:
+    if stop < 0:
         stop += n
+        if stop < 0:
+            stop = 0
+    elif stop >= n:
+        stop = n
 
     step = key.step or 1
     if step != 1:
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index 4c14c1c..45b3f9e 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -132,17 +132,18 @@ def test_array_slice():
 
     # Test slice notation
     assert arr[2:].equals(arr.slice(2))
-
     assert arr[2:5].equals(arr.slice(2, 3))
-
     assert arr[-5:].equals(arr.slice(len(arr) - 5))
-
     with pytest.raises(IndexError):
         arr[::-1]
-
     with pytest.raises(IndexError):
         arr[::2]
 
+    n = len(arr)
+    for start in range(-n * 2, n * 2):
+        for stop in range(-n * 2, n * 2):
+            assert arr[start:stop].to_pylist() == arr.to_pylist()[start:stop]
+
 
 def test_array_factory_invalid_type():
     arr = np.array([datetime.timedelta(1), datetime.timedelta(2)])

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to