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 (pandas-dev#24985)
  • Loading branch information
qwhelan authored and Pingviinituutti committed Feb 28, 2019
1 parent 04d38d1 commit 0b135c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
9 changes: 8 additions & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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`)
-
-

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 0b135c3

Please sign in to comment.