[Python] BUG: Empty slicing an array backwards beyond the start should be empty
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
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)
(and so this case actually worked before, so going to mark as critical for 16.0)
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.
I am moving this to 17.0.0. Let me know if this is required for the 16.0.0 release.
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.
Resolved by https://github.com/apache/arrow/pull/40682
@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 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.