Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,9 @@ def _normalize_slice(object arrow_obj, slice key):
start, stop, step = key.indices(n)

if step != 1:
indices = np.arange(start, stop, step)
indices = list(range(start, stop, step))
if len(indices) == 0:
return arrow_obj.slice(0, 0)
return arrow_obj.take(indices)
else:
length = max(stop - start, 0)
Expand Down
11 changes: 5 additions & 6 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,14 @@ def test_array_slice():
assert res.to_numpy().tolist() == expected


@pytest.mark.numpy
def test_array_slice_negative_step():
# ARROW-2714
np_arr = np.arange(20)
arr = pa.array(np_arr)
values = list(range(20))
arr = pa.array(values)
chunked_arr = pa.chunked_array([arr])

cases = [
slice(None, None, -1),
slice(None, None, -1), # GH-46606
slice(None, 6, -2),
slice(10, 6, -2),
slice(8, None, -2),
Expand All @@ -510,15 +509,15 @@ def test_array_slice_negative_step():

for case in cases:
result = arr[case]
expected = pa.array(np_arr[case])
expected = pa.array(values[case], type=arr.type)
assert result.equals(expected)

result = pa.record_batch([arr], names=['f0'])[case]
expected = pa.record_batch([expected], names=['f0'])
assert result.equals(expected)

result = chunked_arr[case]
expected = pa.chunked_array([np_arr[case]])
expected = pa.chunked_array([values[case]], type=arr.type)
assert result.equals(expected)


Expand Down
Loading