@@ -772,6 +772,7 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
772772
773773 self .engine = engine # remove for 0.12
774774 self .obj = obj
775+
775776 self .path_or_buf = path_or_buf
776777 self .sep = sep
777778 self .na_rep = na_rep
@@ -789,13 +790,27 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
789790
790791 self .line_terminator = line_terminator
791792
792- if cols is None :
793- cols = obj .columns
793+ #GH3457
794+ if not self .obj .columns .is_unique and engine == 'python' :
795+ msg = "columns.is_unique == False not supported with engine='python'"
796+ raise NotImplementedError (msg )
794797
798+ if cols is not None :
799+ if isinstance (cols ,Index ):
800+ cols = cols .to_native_types (na_rep = na_rep ,float_format = float_format )
801+ else :
802+ cols = list (cols )
803+ self .obj = self .obj .loc [:,cols ]
804+
805+ # update columns to include possible multiplicity of dupes
806+ # and make sure sure cols is just a list of labels
807+ cols = self .obj .columns
795808 if isinstance (cols ,Index ):
796809 cols = cols .to_native_types (na_rep = na_rep ,float_format = float_format )
797810 else :
798811 cols = list (cols )
812+
813+ # save it
799814 self .cols = cols
800815
801816 # preallocate data 2d list
@@ -804,7 +819,7 @@ def __init__(self, obj, path_or_buf, sep=",", na_rep='', float_format=None,
804819 self .data = [None ] * ncols
805820
806821 if self .obj .columns .is_unique :
807- self .colname_map = dict ((k ,i ) for i ,k in enumerate (obj .columns ))
822+ self .colname_map = dict ((k ,i ) for i ,k in enumerate (self . obj .columns ))
808823 else :
809824 ks = [set (x .items ) for x in self .blocks ]
810825 u = len (reduce (lambda a ,x : a .union (x ),ks ,set ()))
@@ -1024,7 +1039,9 @@ def _save_chunk(self, start_i, end_i):
10241039 # self.data is a preallocated list
10251040 self .data [self .colname_map [k ]] = d [j ]
10261041 else :
1027- for i in range (len (self .cols )):
1042+ # self.obj should contain a proper view of the dataframes
1043+ # with the specified ordering of cols if cols was specified
1044+ for i in range (len (self .obj .columns )):
10281045 self .data [i ] = self .obj .icol (i ).values [slicer ].tolist ()
10291046
10301047 ix = data_index .to_native_types (slicer = slicer , na_rep = self .na_rep , float_format = self .float_format )
0 commit comments