Skip to content

Commit 862c284

Browse files
authored
PERF: Improve performance in to_csv with date format for index (#44908)
1 parent 66d33b2 commit 862c284

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

asv_bench/benchmarks/io/csv.py

+12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ def time_frame_date_formatting(self):
6767
self.data.to_csv(self.fname, date_format="%Y%m%d")
6868

6969

70+
class ToCSVDatetimeIndex(BaseIO):
71+
72+
fname = "__test__.csv"
73+
74+
def setup(self):
75+
rng = date_range("2000", periods=100_000, freq="S")
76+
self.data = DataFrame({"a": 1}, index=rng)
77+
78+
def time_frame_date_formatting_index(self):
79+
self.data.to_csv(self.fname, date_format="%Y-%m-%d %H:%M:%S")
80+
81+
7082
class ToCSVDatetimeBig(BaseIO):
7183

7284
fname = "__test__.csv"

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ Performance improvements
589589
- Performance improvement in :meth:`Series.to_frame` (:issue:`43558`)
590590
- Performance improvement in :meth:`Series.mad` (:issue:`43010`)
591591
- Performance improvement in :func:`merge` (:issue:`43332`)
592+
- Performance improvement in :func:`to_csv` when index column is a datetime and is formatted (:issue:`39413`)
592593
- Performance improvement in :func:`read_csv` when ``index_col`` was set with a numeric column (:issue:`44158`)
593594
- Performance improvement in :func:`concat` (:issue:`43354`)
594595
-

pandas/io/formats/csvs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
StorageOptions,
2727
WriteBuffer,
2828
)
29+
from pandas.util._decorators import cache_readonly
2930

3031
from pandas.core.dtypes.generic import (
3132
ABCDatetimeIndex,
@@ -175,7 +176,7 @@ def _number_format(self) -> dict[str, Any]:
175176
"decimal": self.decimal,
176177
}
177178

178-
@property
179+
@cache_readonly
179180
def data_index(self) -> Index:
180181
data_index = self.obj.index
181182
if (

0 commit comments

Comments
 (0)