8000 PERF: optimize DataFrame.sparse.from_spmatrix performance by rth · Pull Request #32825 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

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
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
Rename variable
  • Loading branch information
rth committed Mar 19, 2020
commit e541b0dbcfa57d66dc10466d853338eab8ad4ccb
6 changes: 3 additions & 3 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ def from_spmatrix(cls, data, index=None, columns=None):
data.sort_indices()
Copy link
Contributor Author
@rth rth Mar 19, 2020

Choose a reason for hiding this comment

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

It might be already done in tocsc, but that's a scipy implementation detail, and it doesn't really cost much. We need to make sure indices are sorted, since we create IntIndex with check_integrity=False that used to check for this.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment about that inline?

indices = data.indices
indptr = data.indptr
data = data.data
dtype = SparseDtype(data.dtype, 0)
array_data = data.data
dtype = SparseDtype(array_data.dtype, 0)
arrays = []
for i in range(n_columns):
sl = slice(indptr[i], indptr[i + 1])
idx = IntIndex(n_rows, indices[sl], check_integrity=False)
arr = SparseArray._simple_new(data[sl], idx, dtype)
arr = SparseArray._simple_new(array_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)
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)


Expand Down
0