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
Use absolute import
  • Loading branch information
rth committed Mar 19, 2020
commit 42792a396dc51b5cfd1a75c30207ccd150e20341
4 changes: 2 additions & 2 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def from_spmatrix(cls, data, index=None, columns=None):
1 0.0 1.0 0.0
2 0.0 0.0 1.0
"""
from pandas import DataFrame, SparseDtype
from . import IntIndex, SparseArray
from pandas import DataFrame
from pandas._libs.sparse import IntIndex

data = data.tocsc()
Copy link
Contributor Author

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.

index, columns = cls._prep_index(data, index, columns)
Copy link
Member
@jorisvandenbossche jorisvandenbossche Mar 22, 2020

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 doing verify_integrity=False)

Copy link
Contributor Author

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!

Copy link
Member
@jorisvandenbossche jorisvandenbossche Mar 22, 2020

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 of pd.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)

Expand Down
0