From b9df1e60a3b5704d22072e58dab2c476cdffad8e Mon Sep 17 00:00:00 2001 From: LucasG0 Date: Mon, 25 Mar 2024 01:04:41 +0100 Subject: [PATCH] Use zero-copy slice when step=1 --- python/pyarrow/array.pxi | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index 131a9e2b993c3..45fd29ad3b3f3 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -562,8 +562,13 @@ def _normalize_slice(object arrow_obj, slice key): Py_ssize_t n = len(arrow_obj) start, stop, step = key.indices(n) - indices = np.arange(start, stop, step) - return arrow_obj.take(indices) + + if step != 1: + indices = np.arange(start, stop, step) + return arrow_obj.take(indices) + else: + length = max(stop - start, 0) + return arrow_obj.slice(start, length) cdef Py_ssize_t _normalize_index(Py_ssize_t index,