Skip to content

Commit 621891b

Browse files
authored
GH:624 - added mode keyword to DataFrame.to_json (#684)
* added `mode` keyword to `DataFrame.to_json` * Update base.pyi * added overloads and tests * added warnings * done required changes * Update series.pyi * removed the test
1 parent 002f0cc commit 621891b

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,19 +2082,39 @@ class DataFrame(NDFrame, OpsMixin):
20822082
def to_json(
20832083
self,
20842084
path_or_buf: FilePath | WriteBuffer[str],
2085-
orient: JsonFrameOrient | None = ...,
2085+
*,
2086+
orient: Literal["records"],
20862087
date_format: Literal["epoch", "iso"] | None = ...,
20872088
double_precision: int = ...,
20882089
force_ascii: _bool = ...,
20892090
date_unit: Literal["s", "ms", "us", "ns"] = ...,
20902091
default_handler: Callable[[Any], _str | float | _bool | list | dict]
20912092
| None = ...,
2092-
lines: _bool = ...,
2093+
lines: Literal[True],
20932094
compression: CompressionOptions = ...,
20942095
index: _bool = ...,
20952096
indent: int | None = ...,
2097+
mode: Literal["a"],
20962098
) -> None: ...
20972099
@overload
2100+
def to_json(
2101+
self,
2102+
path_or_buf: None = ...,
2103+
*,
2104+
orient: Literal["records"],
2105+
date_format: Literal["epoch", "iso"] | None = ...,
2106+
double_precision: int = ...,
2107+
force_ascii: _bool = ...,
2108+
date_unit: Literal["s", "ms", "us", "ns"] = ...,
2109+
default_handler: Callable[[Any], _str | float | _bool | list | dict]
2110+
| None = ...,
2111+
lines: Literal[True],
2112+
compression: CompressionOptions = ...,
2113+
index: _bool = ...,
2114+
indent: int | None = ...,
2115+
mode: Literal["a"],
2116+
) -> _str: ...
2117+
@overload
20982118
def to_json(
20992119
self,
21002120
path_or_buf: None = ...,
@@ -2109,8 +2129,26 @@ class DataFrame(NDFrame, OpsMixin):
21092129
compression: CompressionOptions = ...,
21102130
index: _bool = ...,
21112131
indent: int | None = ...,
2132+
mode: Literal["w"] = ...,
21122133
) -> _str: ...
21132134
@overload
2135+
def to_json(
2136+
self,
2137+
path_or_buf: FilePath | WriteBuffer[str],
2138+
orient: JsonFrameOrient | None = ...,
2139+
date_format: Literal["epoch", "iso"] | None = ...,
2140+
double_precision: int = ...,
2141+
force_ascii: _bool = ...,
2142+
date_unit: Literal["s", "ms", "us", "ns"] = ...,
2143+
default_handler: Callable[[Any], _str | float | _bool | list | dict]
2144+
| None = ...,
2145+
lines: _bool = ...,
2146+
compression: CompressionOptions = ...,
2147+
index: _bool = ...,
2148+
indent: int | None = ...,
2149+
mode: Literal["w"] = ...,
2150+
) -> None: ...
2151+
@overload
21142152
def to_string(
21152153
self,
21162154
buf: FilePath | WriteBuffer[str],

pandas-stubs/core/series.pyi

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,42 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
484484
encoding: _str | None = ...,
485485
) -> _str: ...
486486
@overload
487+
def to_json(
488+
self,
489+
path_or_buf: FilePath | WriteBuffer[str],
490+
*,
491+
orient: Literal["records"],
492+
date_format: Literal["epoch", "iso"] | None = ...,
493+
double_precision: int = ...,
494+
force_ascii: _bool = ...,
495+
date_unit: Literal["s", "ms", "us", "ns"] = ...,
496+
default_handler: Callable[[Any], _str | float | _bool | list | dict]
497+
| None = ...,
498+
lines: Literal[True],
499+
compression: CompressionOptions = ...,
500+
index: _bool = ...,
501+
indent: int | None = ...,
502+
mode: Literal["a"],
503+
) -> None: ...
504+
@overload
505+
def to_json(
506+
self,
507+
path_or_buf: None = ...,
508+
*,
509+
orient: Literal["records"],
510+
date_format: Literal["epoch", "iso"] | None = ...,
511+
double_precision: int = ...,
512+
force_ascii: _bool = ...,
513+
date_unit: Literal["s", "ms", "us", "ns"] = ...,
514+
default_handler: Callable[[Any], _str | float | _bool | list | dict]
515+
| None = ...,
516+
lines: Literal[True],
517+
compression: CompressionOptions = ...,
518+
index: _bool = ...,
519+
indent: int | None = ...,
520+
mode: Literal["a"],
521+
) -> _str: ...
522+
@overload
487523
def to_json(
488524
self,
489525
path_or_buf: FilePath | WriteBuffer[str],
@@ -498,6 +534,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
498534
compression: CompressionOptions = ...,
499535
index: _bool = ...,
500536
indent: int | None = ...,
537+
mode: Literal["w"] = ...,
501538
) -> None: ...
502539
@overload
503540
def to_json(
@@ -514,6 +551,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
514551
compression: CompressionOptions = ...,
515552
index: _bool = ...,
516553
indent: int | None = ...,
554+
mode: Literal["w"] = ...,
517555
) -> _str: ...
518556
def to_xarray(self) -> xr.DataArray: ...
519557
def items(self) -> Iterable[tuple[Hashable, S1]]: ...

tests/test_frame.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,3 +2677,21 @@ def test_convert_dtypes_dtype_backend() -> None:
26772677
df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [3, 4, 5, 6]})
26782678
dfn = df.convert_dtypes(dtype_backend="numpy_nullable")
26792679
check(assert_type(dfn, pd.DataFrame), pd.DataFrame)
2680+
2681+
2682+
def test_to_json_mode() -> None:
2683+
df = pd.DataFrame(
2684+
[["a", "b"], ["c", "d"]],
2685+
index=["row 1", "row 2"],
2686+
columns=["col 1", "col 2"],
2687+
)
2688+
result = df.to_json(orient="records", lines=True, mode="a")
2689+
result1 = df.to_json(orient="split", mode="w")
2690+
result2 = df.to_json(orient="columns", mode="w")
2691+
result4 = df.to_json(orient="records", mode="w")
2692+
check(assert_type(result, str), str)
2693+
check(assert_type(result1, str), str)
2694+
check(assert_type(result2, str), str)
2695+
check(assert_type(result4, str), str)
2696+
if TYPE_CHECKING_INVALID_USAGE:
2697+
result3 = df.to_json(orient="records", lines=False, mode="a") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]

tests/test_series.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,3 +1966,17 @@ def test_loc_callable() -> None:
19661966
# GH 586
19671967
s = pd.Series([1, 2])
19681968
check(assert_type(s.loc[lambda x: x > 1], pd.Series), pd.Series)
1969+
1970+
1971+
def test_to_json_mode() -> None:
1972+
s = pd.Series([1, 2, 3, 4])
1973+
result = s.to_json(orient="records", lines=True, mode="a")
1974+
result1 = s.to_json(orient="split", mode="w")
1975+
result2 = s.to_json(orient="table", mode="w")
1976+
result4 = s.to_json(orient="records", mode="w")
1977+
check(assert_type(result, str), str)
1978+
check(assert_type(result1, str), str)
1979+
check(assert_type(result2, str), str)
1980+
check(assert_type(result4, str), str)
1981+
if TYPE_CHECKING_INVALID_USAGE:
1982+
result3 = s.to_json(orient="records", lines=False, mode="a") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]

0 commit comments

Comments
 (0)