diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 483cf659080ea..77c80dcfe7c7e 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -102,6 +102,7 @@ Deprecations ~~~~~~~~~~~~ - Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) +- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/series.py b/pandas/core/series.py index eb5b545092307..27ae5d3a8596d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -46,6 +46,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_nonkeyword_arguments, doc, ) from pandas.util._exceptions import find_stack_level @@ -1488,6 +1489,7 @@ def __repr__(self) -> str: def to_string( self, buf: None = ..., + *, na_rep: str = ..., float_format: str | None = ..., header: bool = ..., @@ -1504,6 +1506,7 @@ def to_string( def to_string( self, buf: FilePath | WriteBuffer[str], + *, na_rep: str = ..., float_format: str | None = ..., header: bool = ..., @@ -1516,6 +1519,9 @@ def to_string( ) -> None: ... + @deprecate_nonkeyword_arguments( + version="3.0.0", allowed_args=["self", "buf"], name="to_string" + ) def to_string( self, buf: FilePath | WriteBuffer[str] | None = None, diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index f91f46c8460c4..7c7069aa74eeb 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -35,6 +35,16 @@ def _three_digit_exp(): class TestDataFrameToStringFormatters: + def test_keyword_deprecation(self): + # GH 57280 + msg = ( + "Starting with pandas version 3.0.0 all arguments of to_string " + "except for the argument 'buf' will be keyword-only." + ) + s = Series(["a", "b"]) + with tm.assert_produces_warning(FutureWarning, match=msg): + s.to_string(None, "NaN") + def test_to_string_masked_ea_with_formatter(self): # GH#39336 df = DataFrame(