From d5c6d11ca97ae956b6e5ce8ea23874052e89a357 Mon Sep 17 00:00:00 2001 From: RajatS Mukherjee Date: Thu, 17 Aug 2023 20:49:27 +0000 Subject: [PATCH 1/2] deprecated nonkeyword arguments --- doc/source/whatsnew/v2.2.0.rst | 2 +- pandas/core/frame.py | 4 ++++ pandas/tests/io/formats/test_to_markdown.py | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index 9295ad6cb9aa6..0409fec35cbc7 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -92,8 +92,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ +- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_markdown` except ``buf``. (:issue:`54229`) - Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_pickle` except ``path``. (:issue:`54229`) -- .. --------------------------------------------------------------------------- .. _whatsnew_220.performance: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 797b2f4ddb45e..5aa4540e737a2 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 @@ -2796,6 +2797,9 @@ def to_feather(self, path: FilePath | WriteBuffer[bytes], **kwargs) -> None: to_feather(self, path, **kwargs) + @deprecate_nonkeyword_arguments( + version="3.0", allowed_args=["self", "buf"], name="to_markdown" + ) @doc( Series.to_markdown, klass=_shared_doc_kwargs["klass"], diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 437f079c5f2f9..85eca834ff0d4 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -1,8 +1,12 @@ -from io import StringIO +from io import ( + BytesIO, + StringIO, +) import pytest import pandas as pd +import pandas._testing as tm pytest.importorskip("tabulate") @@ -88,3 +92,15 @@ def test_showindex_disallowed_in_kwargs(): df = pd.DataFrame([1, 2, 3]) with pytest.raises(ValueError, match="Pass 'index' instead of 'showindex"): df.to_markdown(index=True, showindex=True) + + +def test_markdown_pos_args_deprecatation(): + # GH-54229 + df = pd.DataFrame({"a": [1, 2, 3]}) + msg = ( + r"Starting with pandas version 3.0 all arguments of to_markdown except for the " + r"argument 'buf' will be keyword-only." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + buffer = BytesIO() + df.to_markdown(buffer, "grid") From 5a772b7c4b33e1c137deaa1ebb97aa94068e8717 Mon Sep 17 00:00:00 2001 From: RajatS Mukherjee Date: Fri, 18 Aug 2023 06:07:16 +0000 Subject: [PATCH 2/2] added parameter keywords --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 5c80e743d67b4..564c799d7ab66 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1871,7 +1871,7 @@ def to_markdown( {examples} """ return self.to_frame().to_markdown( - buf, mode, index, storage_options=storage_options, **kwargs + buf, mode=mode, index=index, storage_options=storage_options, **kwargs ) # ----------------------------------------------------------------------