Skip to content

Commit

Permalink
Exploratory commit of what CSVFormatter.save should look like
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel committed Jul 21, 2018
1 parent 648bf4d commit be724fa
Showing 1 changed file with 9 additions and 42 deletions.
51 changes: 9 additions & 42 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CSVFormatter(object):
def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
float_format=None, cols=None, header=True, index=True,
index_label=None, mode='w', nanRep=None, encoding=None,
compression=None, quoting=None, line_terminator='\n',
compression='infer', quoting=None, line_terminator='\n',
chunksize=None, tupleize_cols=False, quotechar='"',
date_format=None, doublequote=True, escapechar=None,
decimal='.'):
Expand Down Expand Up @@ -129,32 +129,12 @@ def save(self):
if encoding is None:
encoding = 'ascii' if compat.compat.PY2 else 'utf-8'

# GH 21227 internal compression is not used when file-like passed.
if self.compression and hasattr(self.path_or_buf, 'write'):
msg = ("compression has no effect when passing file-like "
"object as input.")
warnings.warn(msg, RuntimeWarning, stacklevel=2)

# when zip compression is called.
is_zip = isinstance(self.path_or_buf, ZipFile) or (
not hasattr(self.path_or_buf, 'write')
and self.compression == 'zip')

if is_zip:
# zipfile doesn't support writing string to archive. uses string
# buffer to receive csv writing and dump into zip compression
# file handle. GH 21241, 21118
f = StringIO()
close = False
elif hasattr(self.path_or_buf, 'write'):
f = self.path_or_buf
close = False
else:
f, handles = _get_handle(self.path_or_buf, self.mode,
encoding=encoding,
compression=self.compression)
close = True

f, handles = _get_handle(
path_or_buf=self.path_or_buf,
mode=self.mode,
encoding=encoding,
compression=self.compression,
)
try:
writer_kwargs = dict(lineterminator=self.line_terminator,
delimiter=self.sep, quoting=self.quoting,
Expand All @@ -170,21 +150,8 @@ def save(self):
self._save()

finally:
if is_zip:
# GH 17778 handles zip compression separately.
buf = f.getvalue()
if hasattr(self.path_or_buf, 'write'):
self.path_or_buf.write(buf)
else:
f, handles = _get_handle(self.path_or_buf, self.mode,
encoding=encoding,
compression=self.compression)
f.write(buf)
close = True
if close:
f.close()
for _fh in handles:
_fh.close()
for handle in handles:
handle.close()

def _save_header(self):

Expand Down

0 comments on commit be724fa

Please sign in to comment.