Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH:624 - added mode keyword to DataFrame.to_json #684

Merged
merged 7 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...,
Expand All @@ -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],
Expand Down
38 changes: 38 additions & 0 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
compression: CompressionOptions = ...,
index: _bool = ...,
indent: int | None = ...,
mode: Literal["w"] = ...,
) -> None: ...
@overload
def to_json(
Expand All @@ -516,6 +517,43 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
compression: CompressionOptions = ...,
index: _bool = ...,
indent: int | None = ...,
mode: Literal["w"] = ...,
) -> _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"],
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
) -> _str: ...
def to_xarray(self) -> xr.DataArray: ...
def items(self) -> Iterable[tuple[Hashable, S1]]: ...
Expand Down
21 changes: 21 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2578,3 +2578,24 @@ 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")
check(assert_type(result, str), str)
check(assert_type(result1, str), str)
check(assert_type(result2, str), str)
if TYPE_CHECKING_INVALID_USAGE:
result3 = df.to_json(
orient="records", lines=False, mode="a"
) # pyright: ignore[reportGeneralTypeIssues] # type: ignore[call-overload]
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
result4 = df.to_json(orient="records", mode="w")
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
check(assert_type(result3, Any), str)
check(assert_type(result4, str), str)
ramvikrams marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1832,3 +1832,20 @@ 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")
check(assert_type(result, str), str)
check(assert_type(result1, str), str)
check(assert_type(result2, str), str)
if TYPE_CHECKING_INVALID_USAGE:
result3 = s.to_json(
orient="records", lines=False, mode="a"
) # pyright: ignore[reportGeneralTypeIssues] # type: ignore[call-overload]
result4 = s.to_json(orient="records", mode="w")
check(assert_type(result3, Any), str)
check(assert_type(result4, str), str)