Skip to content

Commit 4e3e1fa

Browse files
committed
Move optimization deeper in the call stack
1 parent 71f92a9 commit 4e3e1fa

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Performance improvements
158158
- Performance improvements to :func:`read_sas` (:issue:`47403`, :issue:`47405`, :issue:`47656`, :issue:`48502`)
159159
- Memory improvement in :meth:`RangeIndex.sort_values` (:issue:`48801`)
160160
- Performance improvement in :class:`DataFrameGroupBy` and :class:`SeriesGroupBy` when ``by`` is a categorical type and ``sort=False`` (:issue:`48976`)
161-
- Performance improvement for :meth:`NDFrame.to_csv` when data frame is sparse (:issue:`41023`)
161+
- Performance improvement for saving to CSV with :class:`CSVFormatter` when data frame is sparse (:issue:`41023`)
162162

163163
.. ---------------------------------------------------------------------------
164164
.. _whatsnew_200.bug_fixes:

pandas/core/generic.py

-2
Original file line numberDiff line numberDiff line change
@@ -3717,8 +3717,6 @@ def to_csv(
37173717
>>> df.to_csv('folder/subfolder/out.csv') # doctest: +SKIP
37183718
"""
37193719
df = self if isinstance(self, ABCDataFrame) else self.to_frame()
3720-
if hasattr(df, "sparse"):
3721-
df = df.sparse.to_dense() # fixes 41023
37223720

37233721
formatter = DataFrameFormatter(
37243722
frame=df,

pandas/io/formats/csvs.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def __init__(
6969
) -> None:
7070
self.fmt = formatter
7171

72-
self.obj = self.fmt.frame
72+
if hasattr(self.fmt.frame, "sparse"):
73+
self.obj = self.fmt.frame.sparse.to_dense()
74+
else:
75+
self.obj = self.fmt.frame
7376

7477
self.filepath_or_buffer = path_or_buf
7578
self.encoding = encoding

0 commit comments

Comments
 (0)