diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index d0ddb6e09d555..a9fa8b2174dd0 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -23,6 +23,13 @@ Other Enhancements - - +.. _whatsnew_0250.performance: + +Performance Improvements +~~~~~~~~~~~~~~~~~~~~~~~~ + - Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`) + + .. _whatsnew_0250.api_breaking: @@ -187,7 +194,7 @@ Reshaping Sparse ^^^^^^ -- +- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`) - - diff --git a/pandas/_libs/sparse.pyx b/pandas/_libs/sparse.pyx index f5980998f6db4..5471c8184e458 100644 --- a/pandas/_libs/sparse.pyx +++ b/pandas/_libs/sparse.pyx @@ -72,9 +72,6 @@ cdef class IntIndex(SparseIndex): A ValueError is raised if any of these conditions is violated. """ - cdef: - int32_t index, prev = -1 - if self.npoints > self.length: msg = ("Too many indices. Expected " "{exp} but found {act}").format( @@ -86,17 +83,15 @@ cdef class IntIndex(SparseIndex): if self.npoints == 0: return - if min(self.indices) < 0: + if self.indices.min() < 0: raise ValueError("No index can be less than zero") - if max(self.indices) >= self.length: + if self.indices.max() >= self.length: raise ValueError("All indices must be less than the length") - for index in self.indices: - if prev != -1 and index <= prev: - raise ValueError("Indices must be strictly increasing") - - prev = index + monotonic = np.all(self.indices[:-1] < self.indices[1:]) + if not monotonic: + raise ValueError("Indices must be strictly increasing") def equals(self, other): if not isinstance(other, IntIndex):