Skip to content

Commit a570f3c

Browse files
committed
Add missing changes from pandas-devgh-21868
1 parent 7c5e18d commit a570f3c

File tree

5 files changed

+86
-38
lines changed

5 files changed

+86
-38
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ Deprecations
383383
- :meth:`MultiIndex.to_hierarchical` is deprecated and will be removed in a future version (:issue:`21613`)
384384
- :meth:`Series.ptp` is deprecated. Use ``numpy.ptp`` instead (:issue:`21614`)
385385
- :meth:`Series.compress` is deprecated. Use ``Series[condition]`` instead (:issue:`18262`)
386+
- The signature in :meth:`Series.to_csv` has been deprecated. Please follow the signature in :meth:`DataFrame.to_csv` instead (:issue:`19715`)
386387

387388
.. _whatsnew_0240.prior_deprecations:
388389

pandas/core/series.py

+28-19
Original file line numberDiff line numberDiff line change
@@ -3764,19 +3764,25 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,
37643764
return result
37653765

37663766
def to_csv(self, *args, **kwargs):
3767-
names = ['path_or_buf', 'sep', 'na_rep', 'float_format', 'columns',
3768-
'header', 'index', 'index_label', 'mode', 'encoding',
3769-
'compression', 'quoting', 'quotechar', 'line_terminator',
3770-
'chunksize', 'tupleize_cols', 'date_format', 'doublequote',
3771-
'escapechar', 'decimal']
3772-
3773-
old_names = ['path_or_buf', 'index', 'sep', 'na_rep', 'float_format',
3774-
'header', 'index_label', 'mode', 'encoding',
3775-
'compression', 'date_format', 'decimal']
3776-
3777-
if 'path' in kwargs:
3778-
warnings.warn("Argument 'path' is now named 'path_or_buf'")
3779-
kwargs['path_or_buf'] = kwargs.pop('path')
3767+
warning_klass = FutureWarning
3768+
stack_level = 2
3769+
3770+
names = ["path_or_buf", "sep", "na_rep", "float_format", "columns",
3771+
"header", "index", "index_label", "mode", "encoding",
3772+
"compression", "quoting", "quotechar", "line_terminator",
3773+
"chunksize", "tupleize_cols", "date_format", "doublequote",
3774+
"escapechar", "decimal"]
3775+
3776+
old_names = ["path_or_buf", "index", "sep", "na_rep", "float_format",
3777+
"header", "index_label", "mode", "encoding",
3778+
"compression", "date_format", "decimal"]
3779+
3780+
if "path" in kwargs:
3781+
warnings.warn("The signature of `Series.to_csv` was aligned "
3782+
"to that of `DataFrame.to_csv`, and argument "
3783+
"'path' will be renamed to 'path_or_buf'.",
3784+
warning_klass, stacklevel=stack_level)
3785+
kwargs["path_or_buf"] = kwargs.pop("path")
37803786

37813787
if len(args) > 1:
37823788
# Either "index" (old signature) or "sep" (new signature) is being
@@ -3785,7 +3791,7 @@ def to_csv(self, *args, **kwargs):
37853791

37863792
if not (is_string_like(maybe_sep) and len(maybe_sep) == 1):
37873793
# old signature
3788-
warnings.warn("The signature of `Series.to_csv` was aligned "
3794+
warnings.warn("The signature of `Series.to_csv` was aligned "
37893795
"to that of `DataFrame.to_csv`. Note that the "
37903796
"order of arguments changed, and the new one "
37913797
"has 'sep' in first place, for which \"{}\" is "
@@ -3794,7 +3800,7 @@ def to_csv(self, *args, **kwargs):
37943800
"to the documentation for `DataFrame.to_csv` "
37953801
"when updating your function "
37963802
"calls.".format(maybe_sep),
3797-
FutureWarning, stacklevel=2)
3803+
warning_klass, stacklevel=stack_level)
37983804
names = old_names
37993805

38003806
pos_args = dict(zip(names[:len(args)], args))
@@ -3805,11 +3811,14 @@ def to_csv(self, *args, **kwargs):
38053811
"({})".format(key, names.index(key)))
38063812
kwargs[key] = pos_args[key]
38073813

3808-
if kwargs.get('header', None) is None:
3809-
warnings.warn("Argument 'header' has changed default value to "
3814+
if kwargs.get("header", None) is None:
3815+
warnings.warn("The signature of `Series.to_csv` was aligned "
3816+
"to that of `DataFrame.to_csv`, and argument "
3817+
"'header' will change its default value to "
38103818
"True: please pass an explicit value to suppress "
3811-
"this warning")
3812-
3819+
"this warning.", warning_klass,
3820+
stacklevel=stack_level)
3821+
kwargs["header"] = False # Backwards compatibility.
38133822
return self.to_frame().to_csv(**kwargs)
38143823

38153824
# This entire method is going to be removed, so the shared docstring

pandas/tests/frame/test_to_csv.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -893,22 +893,27 @@ def test_to_csv_line_terminators(self):
893893

894894
def test_to_csv_from_csv_categorical(self):
895895

896-
# CSV with categoricals should result in the same output as when one
897-
# would add a "normal" Series/DataFrame.
898-
s = Series(pd.Categorical(['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c']))
899-
s2 = Series(['a', 'b', 'b', 'a', 'a', 'c', 'c', 'c'])
896+
# CSV with categoricals should result in the same output
897+
# as when one would add a "normal" Series/DataFrame.
898+
s = Series(pd.Categorical(["a", "b", "b", "a", "a", "c", "c", "c"]))
899+
s2 = Series(["a", "b", "b", "a", "a", "c", "c", "c"])
900900
res = StringIO()
901-
s.to_csv(res)
901+
902+
s.to_csv(res, header=False)
902903
exp = StringIO()
903-
s2.to_csv(exp)
904+
905+
s2.to_csv(exp, header=False)
904906
assert res.getvalue() == exp.getvalue()
905907

906908
df = DataFrame({"s": s})
907909
df2 = DataFrame({"s": s2})
910+
908911
res = StringIO()
909912
df.to_csv(res)
913+
910914
exp = StringIO()
911915
df2.to_csv(exp)
916+
912917
assert res.getvalue() == exp.getvalue()
913918

914919
def test_to_csv_path_is_none(self):

pandas/tests/series/test_io.py

+18
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ def test_from_csv_deprecation(self):
4545
depr_ts = Series.from_csv(path)
4646
assert_series_equal(depr_ts, ts)
4747

48+
@pytest.mark.parametrize("arg", ["path", "header", "both"])
49+
def test_to_csv_deprecation(self, arg):
50+
# see gh-19715
51+
with ensure_clean() as path:
52+
if arg == "path":
53+
kwargs = dict(path=path, header=False)
54+
elif arg == "header":
55+
kwargs = dict(path_or_buf=path)
56+
else: # Both discrepancies match.
57+
kwargs = dict(path=path)
58+
59+
with tm.assert_produces_warning(FutureWarning):
60+
self.ts.to_csv(**kwargs)
61+
62+
# Make sure roundtrip still works.
63+
ts = self.read_csv(path)
64+
assert_series_equal(self.ts, ts, check_names=False)
65+
4866
def test_from_csv(self):
4967

5068
with ensure_clean() as path:

pandas/tests/test_common.py

+28-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
import os
5+
import warnings
56
import collections
67
from functools import partial
78

@@ -14,6 +15,14 @@
1415
import pandas.util.testing as tm
1516

1617

18+
def catch_to_csv_depr():
19+
# Catching warnings because Series.to_csv has
20+
# been deprecated. Remove this context when
21+
# Series.to_csv has been aligned.
22+
23+
return warnings.catch_warnings(record=True)
24+
25+
1726
def test_get_callable_name():
1827
getname = com.get_callable_name
1928

@@ -122,11 +131,12 @@ def test_standardize_mapping():
122131
def test_compression_size(obj, method, compression_only):
123132

124133
with tm.ensure_clean() as filename:
125-
getattr(obj, method)(filename, compression=compression_only)
126-
compressed = os.path.getsize(filename)
127-
getattr(obj, method)(filename, compression=None)
128-
uncompressed = os.path.getsize(filename)
129-
assert uncompressed > compressed
134+
with catch_to_csv_depr():
135+
getattr(obj, method)(filename, compression=compression_only)
136+
compressed = os.path.getsize(filename)
137+
getattr(obj, method)(filename, compression=None)
138+
uncompressed = os.path.getsize(filename)
139+
assert uncompressed > compressed
130140

131141

132142
@pytest.mark.parametrize('obj', [
@@ -139,16 +149,21 @@ def test_compression_size_fh(obj, method, compression_only):
139149

140150
with tm.ensure_clean() as filename:
141151
f, _handles = _get_handle(filename, 'w', compression=compression_only)
142-
with f:
143-
getattr(obj, method)(f)
144-
assert not f.closed
145-
assert f.closed
146-
compressed = os.path.getsize(filename)
152+
153+
with catch_to_csv_depr():
154+
with f:
155+
getattr(obj, method)(f)
156+
assert not f.closed
157+
assert f.closed
158+
compressed = os.path.getsize(filename)
159+
147160
with tm.ensure_clean() as filename:
148161
f, _handles = _get_handle(filename, 'w', compression=None)
149-
with f:
150-
getattr(obj, method)(f)
151-
assert not f.closed
162+
163+
with catch_to_csv_depr():
164+
with f:
165+
getattr(obj, method)(f)
166+
assert not f.closed
152167
assert f.closed
153168
uncompressed = os.path.getsize(filename)
154169
assert uncompressed > compressed

0 commit comments

Comments
 (0)