@@ -30,7 +30,7 @@ class CSVFormatter(object):
30
30
def __init__ (self , obj , path_or_buf = None , sep = "," , na_rep = '' ,
31
31
float_format = None , cols = None , header = True , index = True ,
32
32
index_label = None , mode = 'w' , nanRep = None , encoding = None ,
33
- compression = None , quoting = None , line_terminator = '\n ' ,
33
+ compression = 'infer' , quoting = None , line_terminator = '\n ' ,
34
34
chunksize = None , tupleize_cols = False , quotechar = '"' ,
35
35
date_format = None , doublequote = True , escapechar = None ,
36
36
decimal = '.' ):
@@ -129,32 +129,12 @@ def save(self):
129
129
if encoding is None :
130
130
encoding = 'ascii' if compat .compat .PY2 else 'utf-8'
131
131
132
- # GH 21227 internal compression is not used when file-like passed.
133
- if self .compression and hasattr (self .path_or_buf , 'write' ):
134
- msg = ("compression has no effect when passing file-like "
135
- "object as input." )
136
- warnings .warn (msg , RuntimeWarning , stacklevel = 2 )
137
-
138
- # when zip compression is called.
139
- is_zip = isinstance (self .path_or_buf , ZipFile ) or (
140
- not hasattr (self .path_or_buf , 'write' )
141
- and self .compression == 'zip' )
142
-
143
- if is_zip :
144
- # zipfile doesn't support writing string to archive. uses string
145
- # buffer to receive csv writing and dump into zip compression
146
- # file handle. GH 21241, 21118
147
- f = StringIO ()
148
- close = False
149
- elif hasattr (self .path_or_buf , 'write' ):
150
- f = self .path_or_buf
151
- close = False
152
- else :
153
- f , handles = _get_handle (self .path_or_buf , self .mode ,
154
- encoding = encoding ,
155
- compression = self .compression )
156
- close = True
157
-
132
+ f , handles = _get_handle (
133
+ path_or_buf = self .path_or_buf ,
134
+ mode = self .mode ,
135
+ encoding = encoding ,
136
+ compression = self .compression ,
137
+ )
158
138
try :
159
139
writer_kwargs = dict (lineterminator = self .line_terminator ,
160
140
delimiter = self .sep , quoting = self .quoting ,
@@ -170,21 +150,8 @@ def save(self):
170
150
self ._save ()
171
151
172
152
finally :
173
- if is_zip :
174
- # GH 17778 handles zip compression separately.
175
- buf = f .getvalue ()
176
- if hasattr (self .path_or_buf , 'write' ):
177
- self .path_or_buf .write (buf )
178
- else :
179
- f , handles = _get_handle (self .path_or_buf , self .mode ,
180
- encoding = encoding ,
181
- compression = self .compression )
182
- f .write (buf )
183
- close = True
184
- if close :
185
- f .close ()
186
- for _fh in handles :
187
- _fh .close ()
153
+ for handle in handles :
154
+ handle .close ()
188
155
189
156
def _save_header (self ):
190
157
0 commit comments