Skip to content

Commit 191d2ab

Browse files
committed
API: Deprecate old Series.to_csv signature
closes pandas-dev#19715
1 parent a6c7387 commit 191d2ab

File tree

4 files changed

+162
-156
lines changed

4 files changed

+162
-156
lines changed

pandas/core/frame.py

-97
Original file line numberDiff line numberDiff line change
@@ -1712,103 +1712,6 @@ def to_panel(self):
17121712

17131713
return self._constructor_expanddim(new_mgr)
17141714

1715-
def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
1716-
columns=None, header=True, index=True, index_label=None,
1717-
mode='w', encoding=None, compression=None, quoting=None,
1718-
quotechar='"', line_terminator='\n', chunksize=None,
1719-
tupleize_cols=None, date_format=None, doublequote=True,
1720-
escapechar=None, decimal='.'):
1721-
r"""Write DataFrame to a comma-separated values (csv) file
1722-
1723-
Parameters
1724-
----------
1725-
path_or_buf : string or file handle, default None
1726-
File path or object, if None is provided the result is returned as
1727-
a string.
1728-
sep : character, default ','
1729-
Field delimiter for the output file.
1730-
na_rep : string, default ''
1731-
Missing data representation
1732-
float_format : string, default None
1733-
Format string for floating point numbers
1734-
columns : sequence, optional
1735-
Columns to write
1736-
header : boolean or list of string, default True
1737-
Write out the column names. If a list of strings is given it is
1738-
assumed to be aliases for the column names
1739-
index : boolean, default True
1740-
Write row names (index)
1741-
index_label : string or sequence, or False, default None
1742-
Column label for index column(s) if desired. If None is given, and
1743-
`header` and `index` are True, then the index names are used. A
1744-
sequence should be given if the DataFrame uses MultiIndex. If
1745-
False do not print fields for index names. Use index_label=False
1746-
for easier importing in R
1747-
mode : str
1748-
Python write mode, default 'w'
1749-
encoding : string, optional
1750-
A string representing the encoding to use in the output file,
1751-
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
1752-
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default None
1753-
If 'infer' and `path_or_buf` is path-like, then detect compression
1754-
from the following extensions: '.gz', '.bz2' or '.xz'
1755-
(otherwise no compression).
1756-
line_terminator : string, default ``'\n'``
1757-
The newline character or character sequence to use in the output
1758-
file
1759-
quoting : optional constant from csv module
1760-
defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`
1761-
then floats are converted to strings and thus csv.QUOTE_NONNUMERIC
1762-
will treat them as non-numeric
1763-
quotechar : string (length 1), default '\"'
1764-
character used to quote fields
1765-
doublequote : boolean, default True
1766-
Control quoting of `quotechar` inside a field
1767-
escapechar : string (length 1), default None
1768-
character used to escape `sep` and `quotechar` when appropriate
1769-
chunksize : int or None
1770-
rows to write at a time
1771-
tupleize_cols : boolean, default False
1772-
.. deprecated:: 0.21.0
1773-
This argument will be removed and will always write each row
1774-
of the multi-index as a separate row in the CSV file.
1775-
1776-
Write MultiIndex columns as a list of tuples (if True) or in
1777-
the new, expanded format, where each MultiIndex column is a row
1778-
in the CSV (if False).
1779-
date_format : string, default None
1780-
Format string for datetime objects
1781-
decimal: string, default '.'
1782-
Character recognized as decimal separator. E.g. use ',' for
1783-
European data
1784-
1785-
"""
1786-
1787-
if tupleize_cols is not None:
1788-
warnings.warn("The 'tupleize_cols' parameter is deprecated and "
1789-
"will be removed in a future version",
1790-
FutureWarning, stacklevel=2)
1791-
else:
1792-
tupleize_cols = False
1793-
1794-
from pandas.io.formats.csvs import CSVFormatter
1795-
formatter = CSVFormatter(self, path_or_buf,
1796-
line_terminator=line_terminator, sep=sep,
1797-
encoding=encoding,
1798-
compression=compression, quoting=quoting,
1799-
na_rep=na_rep, float_format=float_format,
1800-
cols=columns, header=header, index=index,
1801-
index_label=index_label, mode=mode,
1802-
chunksize=chunksize, quotechar=quotechar,
1803-
tupleize_cols=tupleize_cols,
1804-
date_format=date_format,
1805-
doublequote=doublequote,
1806-
escapechar=escapechar, decimal=decimal)
1807-
formatter.save()
1808-
1809-
if path_or_buf is None:
1810-
return formatter.path_or_buf.getvalue()
1811-
18121715
@Appender(_shared_docs['to_excel'] % _shared_doc_kwargs)
18131716
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
18141717
float_format=None, columns=None, header=True, index=True,

pandas/core/generic.py

+99
Original file line numberDiff line numberDiff line change
@@ -9260,6 +9260,105 @@ def first_valid_index(self):
92609260
def last_valid_index(self):
92619261
return self._find_valid_index('last')
92629262

9263+
def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
9264+
columns=None, header=True, index=True, index_label=None,
9265+
mode='w', encoding=None, compression=None, quoting=None,
9266+
quotechar='"', line_terminator='\n', chunksize=None,
9267+
tupleize_cols=None, date_format=None, doublequote=True,
9268+
escapechar=None, decimal='.'):
9269+
r"""Write DataFrame to a comma-separated values (csv) file
9270+
9271+
Parameters
9272+
----------
9273+
path_or_buf : string or file handle, default None
9274+
File path or object, if None is provided the result is returned as
9275+
a string.
9276+
sep : character, default ','
9277+
Field delimiter for the output file.
9278+
na_rep : string, default ''
9279+
Missing data representation
9280+
float_format : string, default None
9281+
Format string for floating point numbers
9282+
columns : sequence, optional
9283+
Columns to write
9284+
header : boolean or list of string, default True
9285+
Write out the column names. If a list of strings is given it is
9286+
assumed to be aliases for the column names
9287+
index : boolean, default True
9288+
Write row names (index)
9289+
index_label : string or sequence, or False, default None
9290+
Column label for index column(s) if desired. If None is given, and
9291+
`header` and `index` are True, then the index names are used. A
9292+
sequence should be given if the DataFrame uses MultiIndex. If
9293+
False do not print fields for index names. Use index_label=False
9294+
for easier importing in R
9295+
mode : str
9296+
Python write mode, default 'w'
9297+
encoding : string, optional
9298+
A string representing the encoding to use in the output file,
9299+
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
9300+
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default None
9301+
If 'infer' and `path_or_buf` is path-like, then detect compression
9302+
from the following extensions: '.gz', '.bz2' or '.xz'
9303+
(otherwise no compression).
9304+
line_terminator : string, default ``'\n'``
9305+
The newline character or character sequence to use in the output
9306+
file
9307+
quoting : optional constant from csv module
9308+
defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`
9309+
then floats are converted to strings and thus csv.QUOTE_NONNUMERIC
9310+
will treat them as non-numeric
9311+
quotechar : string (length 1), default '\"'
9312+
character used to quote fields
9313+
doublequote : boolean, default True
9314+
Control quoting of `quotechar` inside a field
9315+
escapechar : string (length 1), default None
9316+
character used to escape `sep` and `quotechar` when appropriate
9317+
chunksize : int or None
9318+
rows to write at a time
9319+
tupleize_cols : boolean, default False
9320+
.. deprecated:: 0.21.0
9321+
This argument will be removed and will always write each row
9322+
of the multi-index as a separate row in the CSV file.
9323+
9324+
Write MultiIndex columns as a list of tuples (if True) or in
9325+
the new, expanded format, where each MultiIndex column is a row
9326+
in the CSV (if False).
9327+
date_format : string, default None
9328+
Format string for datetime objects
9329+
decimal: string, default '.'
9330+
Character recognized as decimal separator. E.g. use ',' for
9331+
European data
9332+
9333+
"""
9334+
9335+
df = self if isinstance(self, ABCDataFrame) else self.to_frame()
9336+
9337+
if tupleize_cols is not None:
9338+
warnings.warn("The 'tupleize_cols' parameter is deprecated and "
9339+
"will be removed in a future version",
9340+
FutureWarning, stacklevel=2)
9341+
else:
9342+
tupleize_cols = False
9343+
9344+
from pandas.io.formats.csvs import CSVFormatter
9345+
formatter = CSVFormatter(df, path_or_buf,
9346+
line_terminator=line_terminator, sep=sep,
9347+
encoding=encoding,
9348+
compression=compression, quoting=quoting,
9349+
na_rep=na_rep, float_format=float_format,
9350+
cols=columns, header=header, index=index,
9351+
index_label=index_label, mode=mode,
9352+
chunksize=chunksize, quotechar=quotechar,
9353+
tupleize_cols=tupleize_cols,
9354+
date_format=date_format,
9355+
doublequote=doublequote,
9356+
escapechar=escapechar, decimal=decimal)
9357+
formatter.save()
9358+
9359+
if path_or_buf is None:
9360+
return formatter.path_or_buf.getvalue()
9361+
92639362

92649363
def _doc_parms(cls):
92659364
"""Return a tuple of the doc parms."""

pandas/core/series.py

+54-50
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas.core.arrays import ExtensionArray
1818
from pandas.core.dtypes.common import (
1919
is_categorical_dtype,
20+
is_string_like,
2021
is_bool,
2122
is_integer, is_integer_dtype,
2223
is_float_dtype,
@@ -3762,56 +3763,59 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,
37623763

37633764
return result
37643765

3765-
def to_csv(self, path=None, index=True, sep=",", na_rep='',
3766-
float_format=None, header=False, index_label=None,
3767-
mode='w', encoding=None, compression=None, date_format=None,
3768-
decimal='.'):
3769-
"""
3770-
Write Series to a comma-separated values (csv) file
3771-
3772-
Parameters
3773-
----------
3774-
path : string or file handle, default None
3775-
File path or object, if None is provided the result is returned as
3776-
a string.
3777-
na_rep : string, default ''
3778-
Missing data representation
3779-
float_format : string, default None
3780-
Format string for floating point numbers
3781-
header : boolean, default False
3782-
Write out series name
3783-
index : boolean, default True
3784-
Write row names (index)
3785-
index_label : string or sequence, default None
3786-
Column label for index column(s) if desired. If None is given, and
3787-
`header` and `index` are True, then the index names are used. A
3788-
sequence should be given if the DataFrame uses MultiIndex.
3789-
mode : Python write mode, default 'w'
3790-
sep : character, default ","
3791-
Field delimiter for the output file.
3792-
encoding : string, optional
3793-
a string representing the encoding to use if the contents are
3794-
non-ascii, for python versions prior to 3
3795-
compression : string, optional
3796-
A string representing the compression to use in the output file.
3797-
Allowed values are 'gzip', 'bz2', 'zip', 'xz'. This input is only
3798-
used when the first argument is a filename.
3799-
date_format: string, default None
3800-
Format string for datetime objects.
3801-
decimal: string, default '.'
3802-
Character recognized as decimal separator. E.g. use ',' for
3803-
European data
3804-
"""
3805-
from pandas.core.frame import DataFrame
3806-
df = DataFrame(self)
3807-
# result is only a string if no path provided, otherwise None
3808-
result = df.to_csv(path, index=index, sep=sep, na_rep=na_rep,
3809-
float_format=float_format, header=header,
3810-
index_label=index_label, mode=mode,
3811-
encoding=encoding, compression=compression,
3812-
date_format=date_format, decimal=decimal)
3813-
if path is None:
3814-
return result
3766+
def to_csv(self, *args, **kwargs):
3767+
"""
3768+
See DataFrame.to_csv()
3769+
# TODO
3770+
"""
3771+
3772+
names = ['path_or_buf', 'sep', 'na_rep', 'float_format', 'columns',
3773+
'header', 'index', 'index_label', 'mode', 'encoding',
3774+
'compression', 'quoting', 'quotechar', 'line_terminator',
3775+
'chunksize', 'tupleize_cols', 'date_format', 'doublequote',
3776+
'escapechar', 'decimal']
3777+
3778+
old_names = ['path_or_buf', 'index', 'sep', 'na_rep', 'float_format',
3779+
'header', 'index_label', 'mode', 'encoding',
3780+
'compression', 'date_format', 'decimal']
3781+
3782+
if 'path' in kwargs:
3783+
warnings.warn("Argument 'path' is now named 'path_or_buf'")
3784+
kwargs['path_or_buf'] = kwargs.pop('path')
3785+
3786+
if len(args) > 1:
3787+
# Either "index" (old signature) or "sep" (new signature) is being
3788+
# passed as second argument (while the first is the same)
3789+
maybe_sep = args[1]
3790+
3791+
if not (is_string_like(maybe_sep) and len(maybe_sep) == 1):
3792+
# old signature
3793+
warnings.warn("The signature of `Series.to_csv` was aligned "
3794+
"to that of `DataFrame.to_csv`. Note that the "
3795+
"order of arguments changed, and the new one "
3796+
"has 'sep' in first place, for which \"{}\" is "
3797+
"not a valid value. The old order will cease to "
3798+
"be supported in a future version. Please refer "
3799+
"to the documentation for `DataFrame.to_csv` "
3800+
"when updating your function "
3801+
"calls.".format(maybe_sep),
3802+
FutureWarning, stacklevel=2)
3803+
names = old_names
3804+
3805+
pos_args = dict(zip(names[:len(args)], args))
3806+
3807+
for key in pos_args:
3808+
if key in kwargs:
3809+
raise ValueError("Argument given by name ('{}') and position "
3810+
"({})".format(key, names.index(key)))
3811+
kwargs[key] = pos_args[key]
3812+
3813+
if kwargs.get('header', None) is None:
3814+
warnings.warn("Argument 'header' has changed default value to "
3815+
"True: please pass an explicit value to suppress "
3816+
"this warning")
3817+
3818+
return self.to_frame().to_csv(**kwargs)
38153819

38163820
@Appender(generic._shared_docs['to_excel'] % _shared_doc_kwargs)
38173821
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',

0 commit comments

Comments
 (0)