From 939784f392a421e3caf9e7967349e60c71058852 Mon Sep 17 00:00:00 2001 From: ARF Date: Thu, 7 May 2015 14:29:46 +0200 Subject: [PATCH] PERF: BlockPlacement.copy() Copying of a `BlockPlacement` is currently slower than it could be: * `copy()` cannot be accessed from python, one currently needs to re-implement `copy()` if one wants to duplicate a `BlockPlacement` instance * `copy()` tries to infer a slice for any array contained in the `BlockPlacement`. * After `copy()` passes an array to the constructor, a sanity check in the form of `np.require(val, dtype=np.int64, requirements='W')` checks that the array is a valid input for a `BlockPlacement`. This is unnecessary since the array originated from a `BlockPlacement` for which this check was already done. --- pandas/lib.pyx | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pandas/lib.pyx b/pandas/lib.pyx index cc4c43494176e..fd01e0099ab6b 100644 --- a/pandas/lib.pyx +++ b/pandas/lib.pyx @@ -1641,12 +1641,21 @@ cdef class BlockPlacement: cdef bint _has_slice, _has_array, _is_known_slice_like - def __init__(self, val): + def __init__(self, val, fastpath=False): cdef slice slc self._has_slice = False self._has_array = False + if fastpath: + if isinstance(val, slice): + self._as_slice = val + self._has_slice = True + else: + self._as_array = val + self._has_array = True + return + if isinstance(val, slice): slc = slice_canonize(val) @@ -1783,12 +1792,11 @@ cdef class BlockPlacement: return self - cdef BlockPlacement copy(self): - cdef slice s = self._ensure_has_slice() - if s is not None: - return BlockPlacement(s) + cpdef BlockPlacement copy(self): + if self._as_slice is not None: + return BlockPlacement(self._as_slice, fastpath=True) else: - return BlockPlacement(self._as_array) + return BlockPlacement(self._as_array, fastpath=True) def add(self, other): return self.copy().iadd(other)