Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
PERF: optimize DataFrame.sparse.from_spmatrix performance #32825
Changes from 5 commits
3ffe7f2
93bf825
e21701b
e095e7f
11afe40
eda0732
40f4cd6
508fda5
0053817
42792a3
c8f7abc
e541b0d
0a0bafc
ce6619e
e063c8a
8efe3ad
20c3685
d750eb4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be
tocsc(copy=False)
for newer scipy versions, but the gain in performance is minimal anyway with respect to the total runtime.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem is that this
_pre_index
doesn't actually return Index objects when columns or index is not None (and passing actual Index objects is required when doingverify_integrity=False
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I see. Thanks for confirming that passing duplicate columns names in an Index object is expected to work in
_from_arrays
. Will look into it. Thanks Joris!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems to be it, as doing
pd.DataFrame.sparse.from_spmatrix(mat, columns=pd.Index(['a', 'a']))
instead ofpd.DataFrame.sparse.from_spmatrix(mat, columns=['a', 'a'])
works.You can add in here a
ensure_index
call on both index and columns. (from pandas.core.indexes.api import ensure_index
)There was a problem hiding this comment.
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 createIntIndex
withcheck_integrity=False
that used to check for this.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you assign to a different variable? This in theory could cause mypy complaints (though ndarray resolving to Any at the moment probably allows it to pass)
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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)