Skip to content

Commit 494d0e3

Browse files
authored
GH-46606: [Python] Do not require numpy when normalizing slice (#46732)
### Rationale for this change Slicing an array in non-trivial steps raises an exception when Numpy is not installed. #46606 ### What changes are included in this PR? I changed `np.arange(...)` to `list(range(...))` In `python/pyarrow/array.pxi` ### Are these changes tested? Yes ### Are there any user-facing changes? No * GitHub Issue: #46606 Authored-by: shu-kitamura <[email protected]> Signed-off-by: Raúl Cumplido <[email protected]>
1 parent 30cc5dd commit 494d0e3

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

‎python/pyarrow/array.pxi‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,9 @@ def _normalize_slice(object arrow_obj, slice key):
584584
start, stop, step = key.indices(n)
585585

586586
if step != 1:
587-
indices = np.arange(start, stop, step)
587+
indices = list(range(start, stop, step))
588+
if len(indices) == 0:
589+
return arrow_obj.slice(0, 0)
588590
return arrow_obj.take(indices)
589591
else:
590592
length = max(stop - start, 0)

‎python/pyarrow/tests/test_array.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,14 @@ def test_array_slice():
503503
assert res.to_numpy().tolist() == expected
504504

505505

506-
@pytest.mark.numpy
507506
def test_array_slice_negative_step():
508507
# ARROW-2714
509-
np_arr = np.arange(20)
510-
arr = pa.array(np_arr)
508+
values = list(range(20))
509+
arr = pa.array(values)
511510
chunked_arr = pa.chunked_array([arr])
512511

513512
cases = [
514-
slice(None, None, -1),
513+
slice(None, None, -1), # GH-46606
515514
slice(None, 6, -2),
516515
slice(10, 6, -2),
517516
slice(8, None, -2),
@@ -525,15 +524,15 @@ def test_array_slice_negative_step():
525524

526525
for case in cases:
527526
result = arr[case]
528-
expected = pa.array(np_arr[case])
527+
expected = pa.array(values[case], type=arr.type)
529528
assert result.equals(expected)
530529

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

535534
result = chunked_arr[case]
536-
expected = pa.chunked_array([np_arr[case]])
535+
expected = pa.chunked_array([values[case]], type=arr.type)
537536
assert result.equals(expected)
538537

539538

0 commit comments

Comments
 (0)