Skip to content

Commit 51598aa

Browse files
committed
PERF: significant speedup in sparse init and ops by using numpy in check_integrity
1 parent 2b16e2e commit 51598aa

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

doc/source/whatsnew/v0.24.1.rst

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ including other versions of pandas.
2121
Enhancements
2222
^^^^^^^^^^^^
2323

24+
.. _whatsnew_0241.performance:
25+
26+
Performance Improvements
27+
~~~~~~~~~~~~~~~~~~~~~~~~
28+
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0
29+
2430

2531
.. _whatsnew_0241.bug_fixes:
2632

pandas/_libs/sparse.pyx

+5-10
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ cdef class IntIndex(SparseIndex):
7272
A ValueError is raised if any of these conditions is violated.
7373
"""
7474

75-
cdef:
76-
int32_t index, prev = -1
77-
7875
if self.npoints > self.length:
7976
msg = ("Too many indices. Expected "
8077
"{exp} but found {act}").format(
@@ -86,17 +83,15 @@ cdef class IntIndex(SparseIndex):
8683
if self.npoints == 0:
8784
return
8885

89-
if min(self.indices) < 0:
86+
if self.indices.min() < 0:
9087
raise ValueError("No index can be less than zero")
9188

92-
if max(self.indices) >= self.length:
89+
if self.indices.max() >= self.length:
9390
raise ValueError("All indices must be less than the length")
9491

95-
for index in self.indices:
96-
if prev != -1 and index <= prev:
97-
raise ValueError("Indices must be strictly increasing")
98-
99-
prev = index
92+
monotonic = np.all(self.indices[:-1] < self.indices[1:])
93+
if not monotonic:
94+
raise ValueError("Indices must be strictly increasing")
10095

10196
def equals(self, other):
10297
if not isinstance(other, IntIndex):

0 commit comments

Comments
 (0)