From 5a5687bc5e7aeefc8822c058564f82441a9e36ea Mon Sep 17 00:00:00 2001 From: RajatS Mukherjee Date: Thu, 17 Aug 2023 10:07:29 +0000 Subject: [PATCH 1/2] deprecated nonkeyword arguments --- doc/source/whatsnew/v2.2.0.rst | 2 +- pandas/core/frame.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index c35473b852eb9..92c3f4aa45861 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -92,7 +92,7 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- +- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_string` except ``buf``. (:issue:`54229`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 797b2f4ddb45e..211c0b65046a2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -64,6 +64,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_nonkeyword_arguments, doc, ) from pandas.util._exceptions import find_stack_level @@ -1229,6 +1230,9 @@ def to_string( ) -> None: ... + @deprecate_nonkeyword_arguments( + version=None, allowed_args=["self", "buf"], name="to_string" + ) @Substitution( header_type="bool or list of str", header="Write out the column names. If a list of columns " From 1a6ae307ec428c72df6bc91558ff534c75b3ccee Mon Sep 17 00:00:00 2001 From: RajatS Mukherjee Date: Thu, 17 Aug 2023 16:55:15 +0000 Subject: [PATCH 2/2] added test --- pandas/core/frame.py | 2 +- pandas/tests/io/formats/test_to_string.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 211c0b65046a2..1e10e8f11a575 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1231,7 +1231,7 @@ def to_string( ... @deprecate_nonkeyword_arguments( - version=None, allowed_args=["self", "buf"], name="to_string" + version="3.0", allowed_args=["self", "buf"], name="to_string" ) @Substitution( header_type="bool or list of str", diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index 0c260f0af0a8d..45f3b2201a599 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -11,6 +11,7 @@ option_context, to_datetime, ) +import pandas._testing as tm def test_repr_embedded_ndarray(): @@ -355,3 +356,15 @@ def test_to_string_string_dtype(): z int64[pyarrow]""" ) assert result == expected + + +def test_to_string_pos_args_deprecation(): + # GH-54229 + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"Starting with pandas version 3.0 all arguments of to_string except for the " + r"argument 'buf' will be keyword-only." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + buf = StringIO() + df.to_string(buf, None, None, True, True)