Skip to content

Commit

Permalink
PERF: significant speedup in sparse init and ops by using numpy in ch…
Browse files Browse the repository at this point in the history
…eck_integrity
  • Loading branch information
qwhelan committed Jan 28, 2019
1 parent 2b16e2e commit 51598aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 6 additions & 0 deletions doc/source/whatsnew/v0.24.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ including other versions of pandas.
Enhancements
^^^^^^^^^^^^

.. _whatsnew_0241.performance:

Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Significant speedup in `SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0


.. _whatsnew_0241.bug_fixes:

Expand Down
15 changes: 5 additions & 10 deletions pandas/_libs/sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down

0 comments on commit 51598aa

Please sign in to comment.