Skip to content

PERF: sparse to_csv #49066

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
wants to merge 9 commits into from
20 changes: 20 additions & 0 deletions asv_bench/benchmarks/io/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ def time_head_of_multiindex(self):
self.df_custom_index_then_head.to_csv(self.fname)


class ToCSVSparse(BaseIO):

fname = "__test__.csv"

def setup(self):
from scipy import sparse as sc

vals = np.random.randint(0, 10, size=(500, 1000))
keep = vals > 3
vals[keep] = 0
sparse_mtx = sc.coo_matrix(vals)
self.data = DataFrame.sparse.from_spmatrix(sparse_mtx)

def time_sparse_to_csv(self):
self.data.to_csv(self.fname)

def time_sparse_to_dense_to_csv(self):
self.data.sparse.to_dense().to_csv(self.fname)


class StringIORewind:
def data(self, stringio_object):
stringio_object.seek(0)
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ Performance improvements
- Performance improvement in :func:`read_stata` with parameter ``index_col`` set to ``None`` (the default). Now the index will be a :class:`RangeIndex` instead of :class:`Int64Index` (:issue:`49745`)
- Performance improvement in :func:`merge` when not merging on the index - the new index will now be :class:`RangeIndex` instead of :class:`Int64Index` (:issue:`49478`)
- Performance improvement in :meth:`DataFrame.to_dict` and :meth:`Series.to_dict` when using any non-object dtypes (:issue:`46470`)
- Performance improvement for saving to CSV with :meth:`DataFrame.to_csv` when data frame is sparse (:issue:`41023`)

.. ---------------------------------------------------------------------------
.. _whatsnew_200.bug_fixes:
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
slicer = slice(start_i, end_i)
df = self.obj.iloc[slicer]

# cast sparse columns to dense to reduce calls
# to df._mgr.to_native_types #41023
if hasattr(df, "sparse"):
df = df.sparse.to_dense()

res = df._mgr.to_native_types(**self._number_format)
data = [res.iget_values(i) for i in range(len(res.items))]

Expand Down