From 1b38ede860de6c12224c850409704f4a2fd92127 Mon Sep 17 00:00:00 2001 From: RajatS Mukherjee Date: Fri, 18 Aug 2023 05:59:45 +0000 Subject: [PATCH] deprecated nonkeyword arguments --- doc/source/whatsnew/v2.2.0.rst | 1 + pandas/core/generic.py | 3 +++ pandas/tests/io/json/test_pandas.py | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index 4ad450c965464..2a51f2fa1cc50 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -93,6 +93,7 @@ Other API changes Deprecations ~~~~~~~~~~~~ - Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_hdf` except ``path_or_buf``. (:issue:`54229`) +- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_json` except ``path_or_buf``. (:issue:`54229`) - Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_latex` except ``buf``. (:issue:`54229`) - Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_pickle` except ``path``. (:issue:`54229`) - Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_string` except ``buf``. (:issue:`54229`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index c28ae86985896..7da41b890598d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2352,6 +2352,9 @@ def to_excel( ) @final + @deprecate_nonkeyword_arguments( + version="3.0", allowed_args=["self", "path_or_buf"], name="to_json" + ) @doc( storage_options=_shared_docs["storage_options"], compression_options=_shared_docs["compression_options"] % "path_or_buf", diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index ff9b4acd96499..4ee9e1e2d1598 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -1,7 +1,10 @@ import datetime from datetime import timedelta from decimal import Decimal -from io import StringIO +from io import ( + BytesIO, + StringIO, +) import json import os import sys @@ -2106,3 +2109,15 @@ def test_json_roundtrip_string_inference(orient): columns=pd.Index(["col 1", "col 2"], dtype=pd.ArrowDtype(pa.string())), ) tm.assert_frame_equal(result, expected) + + +def test_json_pos_args_deprecation(): + # GH-54229 + df = DataFrame({"a": [1, 2, 3]}) + msg = ( + r"Starting with pandas version 3.0 all arguments of to_json except for the " + r"argument 'path_or_buf' will be keyword-only." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): + buf = BytesIO() + df.to_json(buf, "split")