Skip to content

PERF: optimize DataFrame.sparse.from_spmatrix performance #32825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add what's new
  • Loading branch information
rth committed Mar 19, 2020
commit 11afe400902d3e7697b95d4e87ff8ece4adc89f8
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ Performance improvements
- The internal index method :meth:`~Index._shallow_copy` now copies cached attributes over to the new index,
avoiding creating these again on the new index. This can speed up many operations that depend on creating copies of
existing indexes (:issue:`28584`, :issue:`32640`, :issue:`32669`)
- Performance improvement when creating sparse :class:`DataFrame` from
``scipy.sparse`` matrices using the :meth:`DataFrame.sparse.from_spmatrix`
constructor (:issue:`32196`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number from joris PR as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added PRs by Joris, there have been 5 PRs on this in total.

Ideally another what's new entry should be added since PR's by @jorisvandenbossche also made initialization of extension arrays faster under some conditions, as far as I understand. Though I would rather not add it here, nor am I competent on accurately formulating it.


.. ---------------------------------------------------------------------------

Expand Down
6 changes: 1 addition & 5 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,7 @@ def from_spmatrix(cls, data, index=None, columns=None):
idx = IntIndex(n_rows, indices[sl], check_integrity=False)
arr = SparseArray._simple_new(data[sl], idx, dtype)
arrays.append(arr)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, also tried with a generator here to avoid pre-allocating all the arrays, but it doesn't really matter. Most of the remaining run time is in DataFrame._from_arrays.

return DataFrame._from_arrays(
arrays,
columns=columns,
index=index
)
return DataFrame._from_arrays(arrays, columns=columns, index=index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will be able to use the verify_integrity=False from #32858 (or if this PR goes in first, I can add it in that PR)


def to_dense(self):
"""
Expand Down