From 50f9ec1c33f4687578aa83f873fb3f1d1c6614fb Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Sat, 7 Jun 2025 02:05:44 +0900 Subject: [PATCH 1/7] Changed from numpy Array to list(range(...)) --- python/pyarrow/array.pxi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index 72856bf1ecc..12aa69567f9 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -584,7 +584,7 @@ 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)) return arrow_obj.take(indices) else: length = max(stop - start, 0) From 0758cb05508f066acfc517d57318eaae4901c15e Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Sat, 7 Jun 2025 10:34:45 +0900 Subject: [PATCH 2/7] add test --- python/pyarrow/tests/test_array.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 1a964cda6c0..0845633c8d7 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -4266,3 +4266,9 @@ def test_non_cpu_array(): arr.tolist() with pytest.raises(NotImplementedError): arr.validate(full=True) + +def test_slicing_with_non_trivial_step(): + # https://github.com/apache/arrow/issues/46606 + arr = pa.array([1.2, 3.5, None]) + assert arr[-1:] == pa.array([None]) + assert arr[::-1] == pa.array([None, 3.5, 1.2]) From 70c876e4b08622080e7235ef2db92d6eac6700f4 Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Sat, 7 Jun 2025 10:40:18 +0900 Subject: [PATCH 3/7] fix test case --- python/pyarrow/tests/test_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 0845633c8d7..0f236ede446 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -4270,5 +4270,5 @@ def test_non_cpu_array(): def test_slicing_with_non_trivial_step(): # https://github.com/apache/arrow/issues/46606 arr = pa.array([1.2, 3.5, None]) - assert arr[-1:] == pa.array([None]) + assert arr[-1:] == pa.array([None], type=pa.float64()) assert arr[::-1] == pa.array([None, 3.5, 1.2]) From acda29bda1c9be4e18ff4cdebcac705a0ea0ae24 Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Sun, 8 Jun 2025 00:31:42 +0900 Subject: [PATCH 4/7] move test case --- python/pyarrow/tests/test_array.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index 0f236ede446..f2286f8e7e3 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -522,6 +522,13 @@ def test_array_slice_negative_step(): assert result.equals(expected) +def test_slicing_with_non_trivial_step(): + # https://github.com/apache/arrow/issues/46606 + arr = pa.array([1.2, 3.5, None]) + assert arr[-1:] == pa.array([None], type=pa.float64()) + assert arr[::-1] == pa.array([None, 3.5, 1.2]) + + def test_array_diff(): # ARROW-6252 arr1 = pa.array(['foo'], type=pa.utf8()) @@ -4266,9 +4273,3 @@ def test_non_cpu_array(): arr.tolist() with pytest.raises(NotImplementedError): arr.validate(full=True) - -def test_slicing_with_non_trivial_step(): - # https://github.com/apache/arrow/issues/46606 - arr = pa.array([1.2, 3.5, None]) - assert arr[-1:] == pa.array([None], type=pa.float64()) - assert arr[::-1] == pa.array([None, 3.5, 1.2]) From 44260b8da4c9d134e22fde21a22695677feb0a40 Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Sun, 8 Jun 2025 11:24:15 +0900 Subject: [PATCH 5/7] Add handling for empty list --- python/pyarrow/array.pxi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index 12aa69567f9..f195305da18 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -585,6 +585,8 @@ def _normalize_slice(object arrow_obj, slice key): if step != 1: 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) From 25904e90403f75843baa91e38811c41f046a9b63 Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Mon, 9 Jun 2025 19:41:36 +0900 Subject: [PATCH 6/7] delete test_slicing_with_non_trivial_step() --- python/pyarrow/tests/test_array.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index f2286f8e7e3..b71e954ac75 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -488,7 +488,6 @@ 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) @@ -496,7 +495,7 @@ def test_array_slice_negative_step(): 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), @@ -522,13 +521,6 @@ def test_array_slice_negative_step(): assert result.equals(expected) -def test_slicing_with_non_trivial_step(): - # https://github.com/apache/arrow/issues/46606 - arr = pa.array([1.2, 3.5, None]) - assert arr[-1:] == pa.array([None], type=pa.float64()) - assert arr[::-1] == pa.array([None, 3.5, 1.2]) - - def test_array_diff(): # ARROW-6252 arr1 = pa.array(['foo'], type=pa.utf8()) From 9b3cb60894abd62d4a0646bfb63b70b25d478785 Mon Sep 17 00:00:00 2001 From: shu-kitamura Date: Mon, 9 Jun 2025 20:39:37 +0900 Subject: [PATCH 7/7] Fixed test cases to not use np.arange --- python/pyarrow/tests/test_array.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py index b71e954ac75..44d796b3c91 100644 --- a/python/pyarrow/tests/test_array.py +++ b/python/pyarrow/tests/test_array.py @@ -490,8 +490,8 @@ def test_array_slice(): 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 = [ @@ -509,7 +509,7 @@ 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] @@ -517,7 +517,7 @@ def test_array_slice_negative_step(): 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)