Skip to content

Commit b9f2e2b

Browse files
qwhelanjreback
authored andcommitted
PERF: significant speedup in sparse init and ops by using numpy in check_integrity (#24985)
1 parent 7486808 commit b9f2e2b

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Diff for: doc/source/whatsnew/v0.25.0.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Other Enhancements
2323
-
2424
-
2525

26+
.. _whatsnew_0250.performance:
27+
28+
Performance Improvements
29+
~~~~~~~~~~~~~~~~~~~~~~~~
30+
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)
31+
32+
2633

2734
.. _whatsnew_0250.api_breaking:
2835

@@ -187,7 +194,7 @@ Reshaping
187194
Sparse
188195
^^^^^^
189196

190-
-
197+
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`)
191198
-
192199
-
193200

Diff for: 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)