diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index c35473b852eb9..9295ad6cb9aa6 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_pickle` except ``path``. (:issue:`54229`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7624c8f7c7930..88fa32ca5b20b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3017,6 +3017,9 @@ def to_sql( ) @final + @deprecate_nonkeyword_arguments( + version=None, allowed_args=["self", "path"], name="to_pickle" + ) @doc( storage_options=_shared_docs["storage_options"], compression_options=_shared_docs["compression_options"] % "path", diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 87beab04bc586..7b5b31037d445 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -460,3 +460,13 @@ def test_bool_dep(self) -> None: ) with tm.assert_produces_warning(FutureWarning, match=msg_warn): DataFrame({"col": [False]}).bool() + + def test_drop_pos_args_deprecation_for_to_pickle(self): + # GH-54229 + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"In a future version of pandas all arguments of to_pickle " + r"except for the argument 'path' will be keyword-only" + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + df.to_pickle("./dummy.pkl", "infer")