Skip to content

Commit bd81fef

Browse files
authored
PERF: Performance Improvement on DataFrame.to_csv() when index=False (#59608)
* add alternative ix when self.nlevel is 0 * add to latest whatsnew * change np.full to np.empty
1 parent 8f7080b commit bd81fef

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ Performance improvements
505505
- Performance improvement in :meth:`DataFrame.join` for sorted but non-unique indexes (:issue:`56941`)
506506
- Performance improvement in :meth:`DataFrame.join` when left and/or right are non-unique and ``how`` is ``"left"``, ``"right"``, or ``"inner"`` (:issue:`56817`)
507507
- Performance improvement in :meth:`DataFrame.join` with ``how="left"`` or ``how="right"`` and ``sort=True`` (:issue:`56919`)
508+
- Performance improvement in :meth:`DataFrame.to_csv` when ``index=False`` (:issue:`59312`)
508509
- Performance improvement in :meth:`DataFrameGroupBy.ffill`, :meth:`DataFrameGroupBy.bfill`, :meth:`SeriesGroupBy.ffill`, and :meth:`SeriesGroupBy.bfill` (:issue:`56902`)
509510
- Performance improvement in :meth:`Index.join` by propagating cached attributes in cases where the result matches one of the inputs (:issue:`57023`)
510511
- Performance improvement in :meth:`Index.take` when ``indices`` is a full range indexer from zero to length of index (:issue:`56806`)

pandas/io/formats/csvs.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
320320
res = df._get_values_for_csv(**self._number_format)
321321
data = list(res._iter_column_arrays())
322322

323-
ix = self.data_index[slicer]._get_values_for_csv(**self._number_format)
323+
ix = (
324+
self.data_index[slicer]._get_values_for_csv(**self._number_format)
325+
if self.nlevels != 0
326+
else np.empty(end_i - start_i)
327+
)
324328
libwriters.write_csv_rows(
325329
data,
326330
ix,

0 commit comments

Comments
 (0)