From 7d8ae2239e0c26008a40ff3f41c2e7e59916fe5b Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 22 Nov 2019 17:40:33 -0800 Subject: [PATCH 1/3] DEPR: Series.to_csv signature change --- pandas/core/series.py | 95 ---------------------------------- pandas/tests/series/test_io.py | 21 +++----- 2 files changed, 6 insertions(+), 110 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 6045d6a654508..fe698eb64e250 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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) - @Appender(generic._shared_docs["isna"] % _shared_doc_kwargs) def isna(self): return super().isna() diff --git a/pandas/tests/series/test_io.py b/pandas/tests/series/test_io.py index f954e6fb4bf98..701e8fad623c3 100644 --- a/pandas/tests/series/test_io.py +++ b/pandas/tests/series/test_io.py @@ -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) def test_from_csv(self, datetime_series, string_series): From 277eede8fa4f60c09ccb3471acbda591be28d03a Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 22 Nov 2019 19:29:30 -0800 Subject: [PATCH 2/3] dummy to force CI From 00257a60fadd7cabfe2db7a60108b31ac89c0bdd Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 24 Nov 2019 11:04:46 -0800 Subject: [PATCH 3/3] remove unnecessary test code --- pandas/tests/io/test_compression.py | 42 +++++++++-------------------- pandas/tests/series/test_io.py | 9 ------- 2 files changed, 13 insertions(+), 38 deletions(-) diff --git a/pandas/tests/io/test_compression.py b/pandas/tests/io/test_compression.py index 9bcdda2039458..54eb2d78fb64f 100644 --- a/pandas/tests/io/test_compression.py +++ b/pandas/tests/io/test_compression.py @@ -1,9 +1,7 @@ -import contextlib import os import subprocess import sys import textwrap -import warnings import pytest @@ -13,17 +11,6 @@ import pandas.io.common as icom -@contextlib.contextmanager -def catch_to_csv_depr(): - # Catching warnings because Series.to_csv has - # been deprecated. Remove this context when - # Series.to_csv has been aligned. - - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", FutureWarning) - yield - - @pytest.mark.parametrize( "obj", [ @@ -37,12 +24,11 @@ def catch_to_csv_depr(): @pytest.mark.parametrize("method", ["to_pickle", "to_json", "to_csv"]) def test_compression_size(obj, method, compression_only): with tm.ensure_clean() as path: - with catch_to_csv_depr(): - getattr(obj, method)(path, compression=compression_only) - compressed_size = os.path.getsize(path) - getattr(obj, method)(path, compression=None) - uncompressed_size = os.path.getsize(path) - assert uncompressed_size > compressed_size + getattr(obj, method)(path, compression=compression_only) + compressed_size = os.path.getsize(path) + getattr(obj, method)(path, compression=None) + uncompressed_size = os.path.getsize(path) + assert uncompressed_size > compressed_size @pytest.mark.parametrize( @@ -59,18 +45,16 @@ def test_compression_size(obj, method, compression_only): def test_compression_size_fh(obj, method, compression_only): with tm.ensure_clean() as path: f, handles = icom._get_handle(path, "w", compression=compression_only) - with catch_to_csv_depr(): - with f: - getattr(obj, method)(f) - assert not f.closed - assert f.closed - compressed_size = os.path.getsize(path) + with f: + getattr(obj, method)(f) + assert not f.closed + assert f.closed + compressed_size = os.path.getsize(path) with tm.ensure_clean() as path: f, handles = icom._get_handle(path, "w", compression=None) - with catch_to_csv_depr(): - with f: - getattr(obj, method)(f) - assert not f.closed + with f: + getattr(obj, method)(f) + assert not f.closed assert f.closed uncompressed_size = os.path.getsize(path) assert uncompressed_size > compressed_size diff --git a/pandas/tests/series/test_io.py b/pandas/tests/series/test_io.py index 701e8fad623c3..cd32b2188b892 100644 --- a/pandas/tests/series/test_io.py +++ b/pandas/tests/series/test_io.py @@ -25,15 +25,6 @@ def read_csv(self, path, **kwargs): return out - def test_to_csv_path_kwarg_raises(self, datetime_series): - # see gh-19715 - with tm.ensure_clean() as path: - with pytest.raises(TypeError): - datetime_series.to_csv(path=path) - - with pytest.raises(TypeError): - datetime_series.to_csv(path=path, header=False) - def test_from_csv(self, datetime_series, string_series): with tm.ensure_clean() as path: