Skip to content

Commit 9bac25c

Browse files
committed
Deprecate signature of Series.to_csv
1 parent 365eac4 commit 9bac25c

File tree

2 files changed

+115
-30
lines changed

2 files changed

+115
-30
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Deprecations
245245
- :meth:`DataFrame.to_stata`, :meth:`read_stata`, :class:`StataReader` and :class:`StataWriter` have deprecated the ``encoding`` argument. The encoding of a Stata dta file is determined by the file type and cannot be changed (:issue:`21244`).
246246
- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`)
247247
- :meth:`Series.ptp` is deprecated. Use ``numpy.ptp`` instead (:issue:`21614`)
248-
-
248+
- The signature in :meth:`Series.to_csv` has been deprecated. Please follow the signature in :meth:`DataFrame.to_csv` instead (:issue:`19745`)
249249

250250
.. _whatsnew_0240.prior_deprecations:
251251

pandas/core/series.py

+114-29
Original file line numberDiff line numberDiff line change
@@ -3760,56 +3760,141 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,
37603760

37613761
return result
37623762

3763-
def to_csv(self, path=None, index=True, sep=",", na_rep='',
3763+
def to_csv_deprecation_decoration(to_csv):
3764+
"""Decorator used to deprecate the signature of to_csv."""
3765+
3766+
from functools import wraps
3767+
3768+
@wraps(to_csv)
3769+
def _to_csv(*args, **kwargs):
3770+
if len(args) > 2:
3771+
warnings.warn(
3772+
"The signature of `Series.to_csv` will be "
3773+
"aligned to that of `DataFrame.to_csv` in the "
3774+
"future. The ordering of positional "
3775+
"arguments is different, so please refer "
3776+
"to the documentation for `DataFrame.to_csv` when "
3777+
"changing your function calls. To ensure "
3778+
"future-proof calls, do not pass more than 1 "
3779+
"positional argument.",
3780+
FutureWarning, stacklevel=2,
3781+
)
3782+
if "path" in kwargs:
3783+
assert len(args) <= 1, (
3784+
"Cannot specify path both as positional "
3785+
"argument and keyword argument."
3786+
)
3787+
assert "path_or_buf" not in kwargs, (
3788+
"Can only specify path or path_or_buf, not both."
3789+
)
3790+
warnings.warn(
3791+
"path is deprecated, use path_or_buf instead",
3792+
FutureWarning, stacklevel=2,
3793+
)
3794+
kwargs["path_or_buf"] = kwargs.pop("path")
3795+
if "header" not in kwargs:
3796+
warnings.warn(
3797+
"The signature of `Series.to_csv` will be "
3798+
"aligned to that of `DataFrame.to_csv` in the "
3799+
"future. The default value of header will change. "
3800+
"To keep the current behaviour, use header=False.",
3801+
FutureWarning, stacklevel=2,
3802+
)
3803+
return to_csv(*args, **kwargs)
3804+
3805+
return _to_csv
3806+
3807+
@to_csv_deprecation_decoration
3808+
def to_csv(self, path_or_buf=None, index=True, sep=",", na_rep='',
37643809
float_format=None, header=False, index_label=None,
37653810
mode='w', encoding=None, compression=None, date_format=None,
3766-
decimal='.'):
3767-
"""
3768-
Write Series to a comma-separated values (csv) file
3811+
decimal='.', columns=None, quoting=None, quotechar='"',
3812+
line_terminator='\n', chunksize=None, doublequote=True,
3813+
escapechar=None):
3814+
3815+
r"""Write Series to a comma-separated values (csv) file
3816+
3817+
.. deprecated:: 0.24.0
3818+
The signature will aligned to that of :func:`DataFrame.to_csv`.
3819+
3820+
:func:`Series.to_csv` will align its signature with that of
3821+
`DataFrame.to_csv`. Please pass in keyword arguments in accordance
3822+
with that signature instead.
37693823
37703824
Parameters
37713825
----------
3772-
path : string or file handle, default None
3826+
path_or_buf : string or file handle, default None
37733827
File path or object, if None is provided the result is returned as
37743828
a string.
3829+
index : boolean, default True
3830+
Write row names (index)
3831+
sep : character, default ','
3832+
Field delimiter for the output file.
37753833
na_rep : string, default ''
37763834
Missing data representation
37773835
float_format : string, default None
37783836
Format string for floating point numbers
3779-
header : boolean, default False
3780-
Write out series name
3781-
index : boolean, default True
3782-
Write row names (index)
3783-
index_label : string or sequence, default None
3837+
header : boolean or list of string, default False
3838+
Write out the column names. If a list of strings is given it is
3839+
assumed to be aliases for the column names
3840+
index_label : string or sequence, or False, default None
37843841
Column label for index column(s) if desired. If None is given, and
37853842
`header` and `index` are True, then the index names are used. A
3786-
sequence should be given if the DataFrame uses MultiIndex.
3787-
mode : Python write mode, default 'w'
3788-
sep : character, default ","
3789-
Field delimiter for the output file.
3843+
sequence should be given if the DataFrame uses MultiIndex. If
3844+
False do not print fields for index names. Use index_label=False
3845+
for easier importing in R
3846+
mode : str
3847+
Python write mode, default 'w'
37903848
encoding : string, optional
3791-
a string representing the encoding to use if the contents are
3792-
non-ascii, for python versions prior to 3
3793-
compression : string, optional
3794-
A string representing the compression to use in the output file.
3795-
Allowed values are 'gzip', 'bz2', 'zip', 'xz'. This input is only
3796-
used when the first argument is a filename.
3797-
date_format: string, default None
3798-
Format string for datetime objects.
3849+
A string representing the encoding to use in the output file,
3850+
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
3851+
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default None
3852+
If 'infer' and `path_or_buf` is path-like, then detect compression
3853+
from the following extensions: '.gz', '.bz2' or '.xz'
3854+
(otherwise no compression).
3855+
date_format : string, default None
3856+
Format string for datetime objects
37993857
decimal: string, default '.'
38003858
Character recognized as decimal separator. E.g. use ',' for
38013859
European data
3860+
columns : sequence, optional
3861+
Columns to write
3862+
quoting : optional constant from csv module
3863+
defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`
3864+
then floats are converted to strings and thus csv.QUOTE_NONNUMERIC
3865+
will treat them as non-numeric
3866+
quotechar : string (length 1), default '\"'
3867+
character used to quote fields
3868+
line_terminator : string, default ``'\n'``
3869+
The newline character or character sequence to use in the output
3870+
file
3871+
chunksize : int or None
3872+
rows to write at a time
3873+
doublequote : boolean, default True
3874+
Control quoting of `quotechar` inside a field
3875+
escapechar : string (length 1), default None
3876+
character used to escape `sep` and `quotechar` when appropriate
3877+
path : string or file handle
3878+
.. deprecated:: 0.21.0
3879+
use `path_or_buf` instead
3880+
38023881
"""
3882+
38033883
from pandas.core.frame import DataFrame
38043884
df = DataFrame(self)
38053885
# result is only a string if no path provided, otherwise None
3806-
result = df.to_csv(path, index=index, sep=sep, na_rep=na_rep,
3807-
float_format=float_format, header=header,
3808-
index_label=index_label, mode=mode,
3809-
encoding=encoding, compression=compression,
3810-
date_format=date_format, decimal=decimal)
3811-
if path is None:
3812-
return result
3886+
result = df.to_csv(
3887+
path_or_buf, index=index, sep=sep, na_rep=na_rep,
3888+
float_format=float_format, header=header,
3889+
index_label=index_label, mode=mode, encoding=encoding,
3890+
compression=compression, date_format=date_format,
3891+
decimal=decimal, columns=columns, quoting=quoting,
3892+
quotechar=quotechar, line_terminator=line_terminator,
3893+
chunksize=chunksize, doublequote=doublequote,
3894+
escapechar=escapechar,
3895+
)
3896+
3897+
return result
38133898

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

0 commit comments

Comments
 (0)