Skip to content

Commit be724fa

Browse files
committed
Exploratory commit of what CSVFormatter.save should look like
1 parent 648bf4d commit be724fa

File tree

1 file changed

+9
-42
lines changed

1 file changed

+9
-42
lines changed

pandas/io/formats/csvs.py

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CSVFormatter(object):
3030
def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
3131
float_format=None, cols=None, header=True, index=True,
3232
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',
3434
chunksize=None, tupleize_cols=False, quotechar='"',
3535
date_format=None, doublequote=True, escapechar=None,
3636
decimal='.'):
@@ -129,32 +129,12 @@ def save(self):
129129
if encoding is None:
130130
encoding = 'ascii' if compat.compat.PY2 else 'utf-8'
131131

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+
)
158138
try:
159139
writer_kwargs = dict(lineterminator=self.line_terminator,
160140
delimiter=self.sep, quoting=self.quoting,
@@ -170,21 +150,8 @@ def save(self):
170150
self._save()
171151

172152
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()
188155

189156
def _save_header(self):
190157

0 commit comments

Comments
 (0)