From 3777313542ad849b5aecfd4bb62488304a62f6ed Mon Sep 17 00:00:00 2001 From: Yoav Ram Date: Fri, 4 Jan 2013 10:08:17 +0200 Subject: [PATCH] added a compression argument to to_csv to be sent to _get_handle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Writing `csv` with `gzip` is very useful as it both minimized the disk size taken by large data files, and can be often read by *R* faster than the same `csv` file without compression.  Also, reading by *R* is done without any effort by the user, and is also already supported in *Pandas* --- pandas/core/frame.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1eaa009275808..767f85d95ede5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1343,7 +1343,7 @@ def _helper_csv(self, writer, na_rep=None, cols=None, def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None, cols=None, header=True, index=True, index_label=None, mode='w', nanRep=None, encoding=None, quoting=None, - line_terminator='\n'): + line_terminator='\n', compression=None): """ Write DataFrame to a comma-separated values (csv) file @@ -1380,6 +1380,10 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None, file quoting : optional constant from csv module defaults to csv.QUOTE_MINIMAL + compression : string, optional + a string representing the compression to use in the output file, + currently only supports gzip and bz2 (see core.common._get_handle), + only used when the first argument is a filename """ if nanRep is not None: # pragma: no cover import warnings @@ -1391,7 +1395,7 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None, f = path_or_buf close = False else: - f = com._get_handle(path_or_buf, mode, encoding=encoding) + f = com._get_handle(path_or_buf, mode, encoding=encoding, compression=compression) close = True if quoting is None: