From f9eef39d2c3a1eb77843ca5b2e43961fc90f6ffd Mon Sep 17 00:00:00 2001 From: chris-b1 Date: Mon, 11 Mar 2019 08:58:12 -0500 Subject: [PATCH] Backport PR #25624: DOC: file obj for to_csv must be newline='' --- doc/source/user_guide/io.rst | 4 ++-- pandas/core/generic.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/source/user_guide/io.rst b/doc/source/user_guide/io.rst index b23a0f10e9e2b..1b5d96fa9c146 100644 --- a/doc/source/user_guide/io.rst +++ b/doc/source/user_guide/io.rst @@ -1689,7 +1689,7 @@ The ``Series`` and ``DataFrame`` objects have an instance method ``to_csv`` whic allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. -* ``path_or_buf``: A string path to the file to write or a StringIO +* ``path_or_buf``: A string path to the file to write or a file object. If a file object it must be opened with `newline=''` * ``sep`` : Field delimiter for the output file (default ",") * ``na_rep``: A string representation of a missing value (default '') * ``float_format``: Format string for floating point numbers @@ -1702,7 +1702,7 @@ function takes a number of arguments. Only the first is required. * ``mode`` : Python write mode, default 'w' * ``encoding``: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 -* ``line_terminator``: Character sequence denoting line end (default '\\n') +* ``line_terminator``: Character sequence denoting line end (default `os.linesep`) * ``quoting``: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a `float_format` then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric * ``quotechar``: Character used to quote fields (default '"') * ``doublequote``: Control quoting of ``quotechar`` in fields (default True) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 45a03f19bb3bb..6b86695a7756e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2895,7 +2895,8 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None, ---------- path_or_buf : str or file handle, default None File path or object, if None is provided the result is returned as - a string. + a string. If a file object is passed it should be opened with + `newline=''`, disabling universal newlines. .. versionchanged:: 0.24.0