@@ -798,6 +798,11 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
798
798
cols = list (cols )
799
799
self .cols = cols
800
800
801
+ # preallocate data 2d list
802
+ self .blocks = self .obj ._data .blocks
803
+ ncols = sum (len (b .items ) for b in self .blocks )
804
+ self .data = [None ] * ncols
805
+
801
806
# fail early if we have duplicate columns
802
807
if len (set (self .cols )) != len (self .cols ):
803
808
raise Exception ("duplicate columns are not permitted in to_csv" )
@@ -1001,18 +1006,17 @@ def _save_chunk(self, start_i, end_i):
1001
1006
data_index = self .data_index
1002
1007
1003
1008
# create the data for a chunk
1004
- blocks = self .obj ._data .blocks
1005
- data = [None ] * sum (len (b .items ) for b in blocks )
1006
1009
slicer = slice (start_i ,end_i )
1007
- for i in range (len (blocks )):
1008
- b = blocks [i ]
1010
+ for i in range (len (self . blocks )):
1011
+ b = self . blocks [i ]
1009
1012
d = b .to_native_types (slicer = slicer , na_rep = self .na_rep , float_format = self .float_format )
1010
1013
for j , k in enumerate (b .items ):
1011
- data [colname_map [k ]] = d [j ]
1014
+ # self.data is a preallocated list
1015
+ self .data [colname_map [k ]] = d [j ]
1012
1016
1013
1017
ix = data_index .to_native_types (slicer = slicer , na_rep = self .na_rep , float_format = self .float_format )
1014
1018
1015
- lib .write_csv_rows (data , ix , self .nlevels , self .cols , self .writer )
1019
+ lib .write_csv_rows (self . data , ix , self .nlevels , self .cols , self .writer )
1016
1020
1017
1021
# from collections import namedtuple
1018
1022
# ExcelCell = namedtuple("ExcelCell",
0 commit comments