Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: Series.to_csv signature change #29809

Merged
merged 5 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 0 additions & 95 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4417,101 +4417,6 @@ def between(self, left, right, inclusive=True):

return lmask & rmask

@Appender(generic.NDFrame.to_csv.__doc__)
def to_csv(self, *args, **kwargs):

names = [
"path_or_buf",
"sep",
"na_rep",
"float_format",
"columns",
"header",
"index",
"index_label",
"mode",
"encoding",
"compression",
"quoting",
"quotechar",
"line_terminator",
"chunksize",
"date_format",
"doublequote",
"escapechar",
"decimal",
]

old_names = [
"path_or_buf",
"index",
"sep",
"na_rep",
"float_format",
"header",
"index_label",
"mode",
"encoding",
"compression",
"date_format",
"decimal",
]

if "path" in kwargs:
warnings.warn(
"The signature of `Series.to_csv` was aligned "
"to that of `DataFrame.to_csv`, and argument "
"'path' will be renamed to 'path_or_buf'.",
FutureWarning,
stacklevel=2,
)
kwargs["path_or_buf"] = kwargs.pop("path")

if len(args) > 1:
# Either "index" (old signature) or "sep" (new signature) is being
# passed as second argument (while the first is the same)
maybe_sep = args[1]

if not (isinstance(maybe_sep, str) and len(maybe_sep) == 1):
# old signature
warnings.warn(
"The signature of `Series.to_csv` was aligned "
"to that of `DataFrame.to_csv`. Note that the "
"order of arguments changed, and the new one "
"has 'sep' in first place, for which \"{}\" is "
"not a valid value. The old order will cease to "
"be supported in a future version. Please refer "
"to the documentation for `DataFrame.to_csv` "
"when updating your function "
"calls.".format(maybe_sep),
FutureWarning,
stacklevel=2,
)
names = old_names

pos_args = dict(zip(names[: len(args)], args))

for key in pos_args:
if key in kwargs:
raise ValueError(
"Argument given by name ('{}') and position "
"({})".format(key, names.index(key))
)
kwargs[key] = pos_args[key]

if kwargs.get("header", None) is None:
warnings.warn(
"The signature of `Series.to_csv` was aligned "
"to that of `DataFrame.to_csv`, and argument "
"'header' will change its default value from False "
"to True: please pass an explicit value to suppress "
"this warning.",
FutureWarning,
stacklevel=2,
)
kwargs["header"] = False # Backwards compatibility.
return self.to_frame().to_csv(**kwargs)

gfyoung marked this conversation as resolved.
Show resolved Hide resolved
@Appender(generic._shared_docs["isna"] % _shared_doc_kwargs)
def isna(self):
return super().isna()
Expand Down
21 changes: 6 additions & 15 deletions pandas/tests/series/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,14 @@ def read_csv(self, path, **kwargs):

return out

@pytest.mark.parametrize("arg", ["path", "header", "both"])
def test_to_csv_deprecation(self, arg, datetime_series):
def test_to_csv_path_kwarg_raises(self, datetime_series):
# see gh-19715
with tm.ensure_clean() as path:
if arg == "path":
kwargs = dict(path=path, header=False)
elif arg == "header":
kwargs = dict(path_or_buf=path)
else: # Both discrepancies match.
kwargs = dict(path=path)

with tm.assert_produces_warning(FutureWarning):
datetime_series.to_csv(**kwargs)

# Make sure roundtrip still works.
ts = self.read_csv(path)
tm.assert_series_equal(datetime_series, ts, check_names=False)
with pytest.raises(TypeError):
datetime_series.to_csv(path=path)

with pytest.raises(TypeError):
datetime_series.to_csv(path=path, header=False)
gfyoung marked this conversation as resolved.
Show resolved Hide resolved

def test_from_csv(self, datetime_series, string_series):

Expand Down