arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Python] Slicing an array backwards beyond the start doesn't include first item

Open jorisvandenbossche opened this issue 3 years ago • 1 comments

From https://github.com/pandas-dev/pandas/pull/55832

When slicing an array "backwards" (with negative step, -1 in the example), and when the stop is "beyond" the start (larger negative number than number of elements), the first item is not included:

In [9]: arr = pa.array([0, 1, 2, 3, 4])

In [10]: arr[4:-6:-1]
Out[10]: 
<pyarrow.lib.Int64Array object at 0x7faab0eaf8e0>
[
  4,
  3,
  2,
  1
]

While if we omit the stop (meaning, slice until the end, in this case of backwards slicing until the start), it works correctly:

In [11]: arr[4::-1]
Out[11]: 
<pyarrow.lib.Int64Array object at 0x7faab0eac340>
[
  4,
  3,
  2,
  1,
  0
]

I haven't yet checked if this is an issue in the underlying C++ slicing, or in the Python layer, eg in our "normalize_slice" logic:

https://github.com/apache/arrow/blob/26cf0e0b154b188499676466579e977829c346f6/python/pyarrow/array.pxi#L544

jorisvandenbossche avatar Nov 17 '23 18:11 jorisvandenbossche

Hi, I am interested in working on this issue. I will have a look and submit a PR.

LucasG0 avatar Dec 07 '23 17:12 LucasG0