arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Python] BUG: Empty slicing an array backwards beyond the start should be empty

Open mroeschke opened this issue 2 years ago • 8 comments

Describe the bug, including details regarding any error messages, version, and platform.

I think post https://github.com/apache/arrow/pull/39240, slicing with an empty slice under the same conditions returns the first element when the result should be empty

In [14]: import pyarrow as pa
    ...: print(pa.__version__)
    ...: array = pa.chunked_array([list("abcde")])
    ...: array[slice(-6, -6, -1)]
16.0.0.dev318
Out[14]: 
<pyarrow.lib.ChunkedArray object at 0x11a1ce4d0>
[
  [
    "a"
  ]
]

In [15]: list("abcde")[slice(-6, -6, -1)]
Out[15]: []

cc @LucasG0 @jorisvandenbossche

Component(s)

Python

mroeschke avatar Mar 18 '24 17:03 mroeschke

Thanks for the catch! Yet another annoying corner case ..

I was wondering if there isn't some existing functionality to "normalize" slice objects, and https://stackoverflow.com/questions/6246084/sanitize-python-slice pointed to https://docs.python.org/3/reference/datamodel.html#slice.indices

We could test if that helps for us. Although for the case above it still gives negative start/stop values .. :

In [3]: slice(-6, -6, -1).indices(5)
Out[3]: (-1, -1, -1)

jorisvandenbossche avatar Mar 19 '24 16:03 jorisvandenbossche

(and so this case actually worked before, so going to mark as critical for 16.0)

jorisvandenbossche avatar Mar 19 '24 16:03 jorisvandenbossche

Using slices.indices seems to be a good call, thanks @jorisvandenbossche. I do not think still having negative values is an issue here, I created https://github.com/apache/arrow/pull/40682.

LucasG0 avatar Mar 20 '24 00:03 LucasG0

I am moving this to 17.0.0. Let me know if this is required for the 16.0.0 release.

raulcd avatar Apr 09 '24 10:04 raulcd

Moved this back to 16.0, because it is a regression that we caused ourselves that for now only lives on main, so we can prevent this gets released. Will look into updating/merging the PR.

jorisvandenbossche avatar Apr 09 '24 12:04 jorisvandenbossche

Resolved by https://github.com/apache/arrow/pull/40682

jorisvandenbossche avatar Apr 09 '24 15:04 jorisvandenbossche

@raulcd not fully familiar with the cherry-picking script, but the PR was pointing to an older already closed issues, and I only renamed it to point to this issue after merging with the merge script. Just mentioning in case that might affect it getting picked up by the backporting.

jorisvandenbossche avatar Apr 09 '24 15:04 jorisvandenbossche

@jorisvandenbossche this has been cherry-picked to the 16.0.0 maintenance branch. The cherry-pick script picked it up without further action on my side.

raulcd avatar Apr 10 '24 08:04 raulcd