Skip to content

Commit d7405e7

Browse files
Moisanvictor
authored and
victor
committed
DOC: Fix DataFrame.to_csv docstring and add an example. GH22459 (pandas-dev#22475)
1 parent ccc73e5 commit d7405e7

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

pandas/core/generic.py

+68-48
Original file line numberDiff line numberDiff line change
@@ -9501,80 +9501,100 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
95019501
quotechar='"', line_terminator='\n', chunksize=None,
95029502
tupleize_cols=None, date_format=None, doublequote=True,
95039503
escapechar=None, decimal='.'):
9504-
r"""Write object to a comma-separated values (csv) file
9504+
r"""
9505+
Write object to a comma-separated values (csv) file.
9506+
9507+
.. versionchanged:: 0.24.0
9508+
The order of arguments for Series was changed.
95059509
95069510
Parameters
95079511
----------
9508-
path_or_buf : string or file handle, default None
9512+
path_or_buf : str or file handle, default None
95099513
File path or object, if None is provided the result is returned as
95109514
a string.
95119515
.. versionchanged:: 0.24.0
95129516
Was previously named "path" for Series.
9513-
sep : character, default ','
9514-
Field delimiter for the output file.
9515-
na_rep : string, default ''
9516-
Missing data representation
9517-
float_format : string, default None
9518-
Format string for floating point numbers
9517+
sep : str, default ','
9518+
String of length 1. Field delimiter for the output file.
9519+
na_rep : str, default ''
9520+
Missing data representation.
9521+
float_format : str, default None
9522+
Format string for floating point numbers.
95199523
columns : sequence, optional
9520-
Columns to write
9521-
header : boolean or list of string, default True
9524+
Columns to write.
9525+
header : bool or list of str, default True
95229526
Write out the column names. If a list of strings is given it is
9523-
assumed to be aliases for the column names
9527+
assumed to be aliases for the column names.
95249528
.. versionchanged:: 0.24.0
95259529
Previously defaulted to False for Series.
9526-
index : boolean, default True
9527-
Write row names (index)
9528-
index_label : string or sequence, or False, default None
9530+
index : bool, default True
9531+
Write row names (index).
9532+
index_label : str or sequence, or False, default None
95299533
Column label for index column(s) if desired. If None is given, and
95309534
`header` and `index` are True, then the index names are used. A
9531-
sequence should be given if the object uses MultiIndex. If
9535+
sequence should be given if the object uses MultiIndex. If
95329536
False do not print fields for index names. Use index_label=False
9533-
for easier importing in R
9537+
for easier importing in R.
95349538
mode : str
9535-
Python write mode, default 'w'
9536-
encoding : string, optional
9539+
Python write mode, default 'w'.
9540+
encoding : str, optional
95379541
A string representing the encoding to use in the output file,
95389542
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
9539-
compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None},
9540-
default 'infer'
9541-
If 'infer' and `path_or_buf` is path-like, then detect compression
9542-
from the following extensions: '.gz', '.bz2', '.zip' or '.xz'
9543-
(otherwise no compression).
9544-
9543+
compression : str, default 'infer'
9544+
Compression mode among the following possible values: {'infer',
9545+
'gzip', 'bz2', 'zip', 'xz', None}. If 'infer' and `path_or_buf`
9546+
is path-like, then detect compression from the following
9547+
extensions: '.gz', '.bz2', '.zip' or '.xz'. (otherwise no
9548+
compression).
95459549
.. versionchanged:: 0.24.0
9546-
'infer' option added and set to default
9547-
line_terminator : string, default ``'\n'``
9548-
The newline character or character sequence to use in the output
9549-
file
9550+
'infer' option added and set to default.
95509551
quoting : optional constant from csv module
9551-
defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`
9552+
Defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`
95529553
then floats are converted to strings and thus csv.QUOTE_NONNUMERIC
9553-
will treat them as non-numeric
9554-
quotechar : string (length 1), default '\"'
9555-
character used to quote fields
9556-
doublequote : boolean, default True
9557-
Control quoting of `quotechar` inside a field
9558-
escapechar : string (length 1), default None
9559-
character used to escape `sep` and `quotechar` when appropriate
9554+
will treat them as non-numeric.
9555+
quotechar : str, default '\"'
9556+
String of length 1. Character used to quote fields.
9557+
line_terminator : string, default ``'\n'``
9558+
The newline character or character sequence to use in the output
9559+
file.
95609560
chunksize : int or None
9561-
rows to write at a time
9562-
tupleize_cols : boolean, default False
9563-
.. deprecated:: 0.21.0
9564-
This argument will be removed and will always write each row
9565-
of the multi-index as a separate row in the CSV file.
9566-
9561+
Rows to write at a time.
9562+
tupleize_cols : bool, default False
95679563
Write MultiIndex columns as a list of tuples (if True) or in
95689564
the new, expanded format, where each MultiIndex column is a row
95699565
in the CSV (if False).
9570-
date_format : string, default None
9571-
Format string for datetime objects
9572-
decimal: string, default '.'
9566+
.. deprecated:: 0.21.0
9567+
This argument will be removed and will always write each row
9568+
of the multi-index as a separate row in the CSV file.
9569+
date_format : str, default None
9570+
Format string for datetime objects.
9571+
doublequote : bool, default True
9572+
Control quoting of `quotechar` inside a field.
9573+
escapechar : str, default None
9574+
String of length 1. Character used to escape `sep` and `quotechar`
9575+
when appropriate.
9576+
decimal : str, default '.'
95739577
Character recognized as decimal separator. E.g. use ',' for
9574-
European data
9578+
European data.
95759579
9576-
.. versionchanged:: 0.24.0
9577-
The order of arguments for Series was changed.
9580+
Returns
9581+
-------
9582+
None or str
9583+
If path_or_buf is None, returns the resulting csv format as a
9584+
string. Otherwise returns None.
9585+
9586+
See Also
9587+
--------
9588+
pandas.read_csv : Load a CSV file into a DataFrame.
9589+
pandas.to_excel: Load an Excel file into a DataFrame.
9590+
9591+
Examples
9592+
--------
9593+
>>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
9594+
... 'mask': ['red', 'purple'],
9595+
... 'weapon': ['sai', 'bo staff']})
9596+
>>> df.to_csv(index=False)
9597+
'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n'
95789598
"""
95799599

95809600
df = self if isinstance(self, ABCDataFrame) else self.to_frame()

0 commit comments

Comments
 (0)