Skip to content

Commit 854bcb5

Browse files
jbrockmendeljreback
authored andcommitted
DEPR: Series.to_csv signature change (#29809)
1 parent 7eb0db3 commit 854bcb5

File tree

3 files changed

+13
-142
lines changed

3 files changed

+13
-142
lines changed

pandas/core/series.py

-95
Original file line numberDiff line numberDiff line change
@@ -4400,101 +4400,6 @@ def between(self, left, right, inclusive=True):
44004400

44014401
return lmask & rmask
44024402

4403-
@Appender(generic.NDFrame.to_csv.__doc__)
4404-
def to_csv(self, *args, **kwargs):
4405-
4406-
names = [
4407-
"path_or_buf",
4408-
"sep",
4409-
"na_rep",
4410-
"float_format",
4411-
"columns",
4412-
"header",
4413-
"index",
4414-
"index_label",
4415-
"mode",
4416-
"encoding",
4417-
"compression",
4418-
"quoting",
4419-
"quotechar",
4420-
"line_terminator",
4421-
"chunksize",
4422-
"date_format",
4423-
"doublequote",
4424-
"escapechar",
4425-
"decimal",
4426-
]
4427-
4428-
old_names = [
4429-
"path_or_buf",
4430-
"index",
4431-
"sep",
4432-
"na_rep",
4433-
"float_format",
4434-
"header",
4435-
"index_label",
4436-
"mode",
4437-
"encoding",
4438-
"compression",
4439-
"date_format",
4440-
"decimal",
4441-
]
4442-
4443-
if "path" in kwargs:
4444-
warnings.warn(
4445-
"The signature of `Series.to_csv` was aligned "
4446-
"to that of `DataFrame.to_csv`, and argument "
4447-
"'path' will be renamed to 'path_or_buf'.",
4448-
FutureWarning,
4449-
stacklevel=2,
4450-
)
4451-
kwargs["path_or_buf"] = kwargs.pop("path")
4452-
4453-
if len(args) > 1:
4454-
# Either "index" (old signature) or "sep" (new signature) is being
4455-
# passed as second argument (while the first is the same)
4456-
maybe_sep = args[1]
4457-
4458-
if not (isinstance(maybe_sep, str) and len(maybe_sep) == 1):
4459-
# old signature
4460-
warnings.warn(
4461-
"The signature of `Series.to_csv` was aligned "
4462-
"to that of `DataFrame.to_csv`. Note that the "
4463-
"order of arguments changed, and the new one "
4464-
"has 'sep' in first place, for which \"{}\" is "
4465-
"not a valid value. The old order will cease to "
4466-
"be supported in a future version. Please refer "
4467-
"to the documentation for `DataFrame.to_csv` "
4468-
"when updating your function "
4469-
"calls.".format(maybe_sep),
4470-
FutureWarning,
4471-
stacklevel=2,
4472-
)
4473-
names = old_names
4474-
4475-
pos_args = dict(zip(names[: len(args)], args))
4476-
4477-
for key in pos_args:
4478-
if key in kwargs:
4479-
raise ValueError(
4480-
"Argument given by name ('{}') and position "
4481-
"({})".format(key, names.index(key))
4482-
)
4483-
kwargs[key] = pos_args[key]
4484-
4485-
if kwargs.get("header", None) is None:
4486-
warnings.warn(
4487-
"The signature of `Series.to_csv` was aligned "
4488-
"to that of `DataFrame.to_csv`, and argument "
4489-
"'header' will change its default value from False "
4490-
"to True: please pass an explicit value to suppress "
4491-
"this warning.",
4492-
FutureWarning,
4493-
stacklevel=2,
4494-
)
4495-
kwargs["header"] = False # Backwards compatibility.
4496-
return self.to_frame().to_csv(**kwargs)
4497-
44984403
@Appender(generic._shared_docs["isna"] % _shared_doc_kwargs)
44994404
def isna(self):
45004405
return super().isna()

pandas/tests/io/test_compression.py

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import contextlib
21
import os
32
import subprocess
43
import sys
54
import textwrap
6-
import warnings
75

86
import pytest
97

@@ -13,17 +11,6 @@
1311
import pandas.io.common as icom
1412

1513

16-
@contextlib.contextmanager
17-
def catch_to_csv_depr():
18-
# Catching warnings because Series.to_csv has
19-
# been deprecated. Remove this context when
20-
# Series.to_csv has been aligned.
21-
22-
with warnings.catch_warnings(record=True):
23-
warnings.simplefilter("ignore", FutureWarning)
24-
yield
25-
26-
2714
@pytest.mark.parametrize(
2815
"obj",
2916
[
@@ -37,12 +24,11 @@ def catch_to_csv_depr():
3724
@pytest.mark.parametrize("method", ["to_pickle", "to_json", "to_csv"])
3825
def test_compression_size(obj, method, compression_only):
3926
with tm.ensure_clean() as path:
40-
with catch_to_csv_depr():
41-
getattr(obj, method)(path, compression=compression_only)
42-
compressed_size = os.path.getsize(path)
43-
getattr(obj, method)(path, compression=None)
44-
uncompressed_size = os.path.getsize(path)
45-
assert uncompressed_size > compressed_size
27+
getattr(obj, method)(path, compression=compression_only)
28+
compressed_size = os.path.getsize(path)
29+
getattr(obj, method)(path, compression=None)
30+
uncompressed_size = os.path.getsize(path)
31+
assert uncompressed_size > compressed_size
4632

4733

4834
@pytest.mark.parametrize(
@@ -59,18 +45,16 @@ def test_compression_size(obj, method, compression_only):
5945
def test_compression_size_fh(obj, method, compression_only):
6046
with tm.ensure_clean() as path:
6147
f, handles = icom._get_handle(path, "w", compression=compression_only)
62-
with catch_to_csv_depr():
63-
with f:
64-
getattr(obj, method)(f)
65-
assert not f.closed
66-
assert f.closed
67-
compressed_size = os.path.getsize(path)
48+
with f:
49+
getattr(obj, method)(f)
50+
assert not f.closed
51+
assert f.closed
52+
compressed_size = os.path.getsize(path)
6853
with tm.ensure_clean() as path:
6954
f, handles = icom._get_handle(path, "w", compression=None)
70-
with catch_to_csv_depr():
71-
with f:
72-
getattr(obj, method)(f)
73-
assert not f.closed
55+
with f:
56+
getattr(obj, method)(f)
57+
assert not f.closed
7458
assert f.closed
7559
uncompressed_size = os.path.getsize(path)
7660
assert uncompressed_size > compressed_size

pandas/tests/series/test_io.py

-18
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ def read_csv(self, path, **kwargs):
2525

2626
return out
2727

28-
@pytest.mark.parametrize("arg", ["path", "header", "both"])
29-
def test_to_csv_deprecation(self, arg, datetime_series):
30-
# see gh-19715
31-
with tm.ensure_clean() as path:
32-
if arg == "path":
33-
kwargs = dict(path=path, header=False)
34-
elif arg == "header":
35-
kwargs = dict(path_or_buf=path)
36-
else: # Both discrepancies match.
37-
kwargs = dict(path=path)
38-
39-
with tm.assert_produces_warning(FutureWarning):
40-
datetime_series.to_csv(**kwargs)
41-
42-
# Make sure roundtrip still works.
43-
ts = self.read_csv(path)
44-
tm.assert_series_equal(datetime_series, ts, check_names=False)
45-
4628
def test_from_csv(self, datetime_series, string_series):
4729

4830
with tm.ensure_clean() as path:

0 commit comments

Comments
 (0)