Skip to content

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

@jorisvandenbossche

Description

@jorisvandenbossche

From pandas-dev/pandas#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:

def _normalize_slice(object arrow_obj, slice key):

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions