Skip to content

ENH: sparse_series_to_coo performance #42880

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

Closed
TLouf opened this issue Aug 4, 2021 · 2 comments · Fixed by #42925
Closed

ENH: sparse_series_to_coo performance #42880

TLouf opened this issue Aug 4, 2021 · 2 comments · Fixed by #42925
Labels
MultiIndex Performance Memory or execution speed performance Sparse Sparse Data Type
Milestone

Comments

@TLouf
Copy link
Contributor

TLouf commented Aug 4, 2021

Is your feature request related to a problem?

Converting a sparse Series to a scipy.sparse.coo_matrix could be much faster. I think the get_indexer function defined in _to_ijv adds unnecessary complexity.

Describe the solution you'd like

It can be much faster by accessing the codes attribute of the multiindex, as follows:

i_coord, j_coord = ss.index.codes
i_labels, j_labels = ss.index.levels

for a two-level multiindex. It should be straightforward to extend to more levels I think.

API breaking implications

None

Describe alternatives you've considered

None

Additional context

To give an example, I started digging into this problem because I had a 2-level-MultiIndexed Series with 61M rows, that is to be converted to a 1M x 1500 sparse matrix. Making the conversion using to_coo() took 10min, making it as described above took half a second.

@TLouf TLouf added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2021
@mzeitlin11
Copy link
Member

Thanks for looking at this @TLouf! Would you be interested in putting up a pr to do as you describe?

@mzeitlin11 mzeitlin11 added MultiIndex Performance Memory or execution speed performance Sparse Sparse Data Type and removed Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Aug 4, 2021
@TLouf
Copy link
Contributor Author

TLouf commented Aug 4, 2021

Yes, I would, I'll write one sometime this week.

@jreback jreback modified the milestones: Contributions Welcome, 1.4 Aug 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MultiIndex Performance Memory or execution speed performance Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants