From 193f5c09b3598d7bcb4de47108d93514176e3eca Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 13 Mar 2020 16:25:50 +0200 Subject: [PATCH 1/3] PERF: Using Numpy C-API arange --- pandas/_libs/internals.pyx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 437406cbbd819..7d45233d92855 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -7,7 +7,9 @@ cdef extern from "Python.h": Py_ssize_t PY_SSIZE_T_MAX import numpy as np -from numpy cimport int64_t +cimport numpy as cnp +from numpy cimport NPY_INT64, int64_t +cnp.import_array() from pandas._libs.algos import ensure_int64 @@ -105,7 +107,7 @@ cdef class BlockPlacement: Py_ssize_t start, stop, end, _ if not self._has_array: start, stop, step, _ = slice_get_indices_ex(self._as_slice) - self._as_array = np.arange(start, stop, step, dtype=np.int64) + self._as_array = cnp.PyArray_Arange(start, stop, step, NPY_INT64) self._has_array = True return self._as_array From addde1e5cf5a3529724cbce7888b1e86d81e60cc Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 13 Mar 2020 18:28:59 +0200 Subject: [PATCH 2/3] Added note REF: https://github.com/pandas-dev/pandas/pull/32681#discussion_r392334261 --- pandas/_libs/internals.pyx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 7d45233d92855..378fc9bb747cd 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -107,6 +107,8 @@ cdef class BlockPlacement: Py_ssize_t start, stop, end, _ if not self._has_array: start, stop, step, _ = slice_get_indices_ex(self._as_slice) + # NOTE: this is the C-optimized equivalent of + # np.arange(start, stop, step, dtype=np.int64) self._as_array = cnp.PyArray_Arange(start, stop, step, NPY_INT64) self._has_array = True return self._as_array From c1f6688f08caa51723ff1bb5e69e52a8cf35d57a Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 13 Mar 2020 19:02:01 +0200 Subject: [PATCH 3/3] Added extra space at the beggining of the comment block REF: https://github.com/pandas-dev/pandas/pull/32681#discussion_r392354159 --- pandas/_libs/internals.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 378fc9bb747cd..7f861e587e637 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -108,7 +108,7 @@ cdef class BlockPlacement: if not self._has_array: start, stop, step, _ = slice_get_indices_ex(self._as_slice) # NOTE: this is the C-optimized equivalent of - # np.arange(start, stop, step, dtype=np.int64) + # np.arange(start, stop, step, dtype=np.int64) self._as_array = cnp.PyArray_Arange(start, stop, step, NPY_INT64) self._has_array = True return self._as_array