diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index b94912442..7fb7e6b3d 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -2012,19 +2012,39 @@ class DataFrame(NDFrame, OpsMixin): def to_json( self, path_or_buf: FilePath | WriteBuffer[str], - orient: JsonFrameOrient | None = ..., + *, + orient: Literal["records"], date_format: Literal["epoch", "iso"] | None = ..., double_precision: int = ..., force_ascii: _bool = ..., date_unit: Literal["s", "ms", "us", "ns"] = ..., default_handler: Callable[[Any], _str | float | _bool | list | dict] | None = ..., - lines: _bool = ..., + lines: Literal[True], compression: CompressionOptions = ..., index: _bool = ..., indent: int | None = ..., + mode: Literal["a"], ) -> None: ... @overload + def to_json( + self, + path_or_buf: None = ..., + *, + orient: Literal["records"], + date_format: Literal["epoch", "iso"] | None = ..., + double_precision: int = ..., + force_ascii: _bool = ..., + date_unit: Literal["s", "ms", "us", "ns"] = ..., + default_handler: Callable[[Any], _str | float | _bool | list | dict] + | None = ..., + lines: Literal[True], + compression: CompressionOptions = ..., + index: _bool = ..., + indent: int | None = ..., + mode: Literal["a"], + ) -> _str: ... + @overload def to_json( self, path_or_buf: None = ..., @@ -2039,8 +2059,26 @@ class DataFrame(NDFrame, OpsMixin): compression: CompressionOptions = ..., index: _bool = ..., indent: int | None = ..., + mode: Literal["w"] = ..., ) -> _str: ... @overload + def to_json( + self, + path_or_buf: FilePath | WriteBuffer[str], + orient: JsonFrameOrient | None = ..., + date_format: Literal["epoch", "iso"] | None = ..., + double_precision: int = ..., + force_ascii: _bool = ..., + date_unit: Literal["s", "ms", "us", "ns"] = ..., + default_handler: Callable[[Any], _str | float | _bool | list | dict] + | None = ..., + lines: _bool = ..., + compression: CompressionOptions = ..., + index: _bool = ..., + indent: int | None = ..., + mode: Literal["w"] = ..., + ) -> None: ... + @overload def to_string( self, buf: FilePath | WriteBuffer[str], diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 41c1e9f27..50216361c 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -486,6 +486,42 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): encoding: _str | None = ..., ) -> _str: ... @overload + def to_json( + self, + path_or_buf: FilePath | WriteBuffer[str], + *, + orient: Literal["records"], + date_format: Literal["epoch", "iso"] | None = ..., + double_precision: int = ..., + force_ascii: _bool = ..., + date_unit: Literal["s", "ms", "us", "ns"] = ..., + default_handler: Callable[[Any], _str | float | _bool | list | dict] + | None = ..., + lines: Literal[True], + compression: CompressionOptions = ..., + index: _bool = ..., + indent: int | None = ..., + mode: Literal["a"], + ) -> None: ... + @overload + def to_json( + self, + path_or_buf: None = ..., + *, + orient: Literal["records"], + date_format: Literal["epoch", "iso"] | None = ..., + double_precision: int = ..., + force_ascii: _bool = ..., + date_unit: Literal["s", "ms", "us", "ns"] = ..., + default_handler: Callable[[Any], _str | float | _bool | list | dict] + | None = ..., + lines: Literal[True], + compression: CompressionOptions = ..., + index: _bool = ..., + indent: int | None = ..., + mode: Literal["a"], + ) -> _str: ... + @overload def to_json( self, path_or_buf: FilePath | WriteBuffer[str], @@ -500,6 +536,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): compression: CompressionOptions = ..., index: _bool = ..., indent: int | None = ..., + mode: Literal["w"] = ..., ) -> None: ... @overload def to_json( @@ -516,6 +553,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): compression: CompressionOptions = ..., index: _bool = ..., indent: int | None = ..., + mode: Literal["w"] = ..., ) -> _str: ... def to_xarray(self) -> xr.DataArray: ... def items(self) -> Iterable[tuple[Hashable, S1]]: ... diff --git a/tests/test_frame.py b/tests/test_frame.py index 94f279563..6d6db78d9 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -2578,3 +2578,21 @@ def test_convert_dtypes_dtype_backend() -> None: df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [3, 4, 5, 6]}) dfn = df.convert_dtypes(dtype_backend="numpy_nullable") check(assert_type(dfn, pd.DataFrame), pd.DataFrame) + + +def test_to_json_mode() -> None: + df = pd.DataFrame( + [["a", "b"], ["c", "d"]], + index=["row 1", "row 2"], + columns=["col 1", "col 2"], + ) + result = df.to_json(orient="records", lines=True, mode="a") + result1 = df.to_json(orient="split", mode="w") + result2 = df.to_json(orient="columns", mode="w") + result4 = df.to_json(orient="records", mode="w") + check(assert_type(result, str), str) + check(assert_type(result1, str), str) + check(assert_type(result2, str), str) + check(assert_type(result4, str), str) + if TYPE_CHECKING_INVALID_USAGE: + result3 = df.to_json(orient="records", lines=False, mode="a") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues] diff --git a/tests/test_series.py b/tests/test_series.py index f76c2662d..51118595d 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -1832,3 +1832,17 @@ def test_loc_callable() -> None: # GH 586 s = pd.Series([1, 2]) check(assert_type(s.loc[lambda x: x > 1], pd.Series), pd.Series) + + +def test_to_json_mode() -> None: + s = pd.Series([1, 2, 3, 4]) + result = s.to_json(orient="records", lines=True, mode="a") + result1 = s.to_json(orient="split", mode="w") + result2 = s.to_json(orient="table", mode="w") + result4 = s.to_json(orient="records", mode="w") + check(assert_type(result, str), str) + check(assert_type(result1, str), str) + check(assert_type(result2, str), str) + check(assert_type(result4, str), str) + if TYPE_CHECKING_INVALID_USAGE: + result3 = s.to_json(orient="records", lines=False, mode="a") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]