Skip to content

Commit 7603dde

Browse files
GHXXX Add overloads of engine for pd.read_json
1 parent 0ab562c commit 7603dde

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

pandas-stubs/io/json/_json.pyi

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,42 @@ def read_json(
4242
Literal["strict", "ignore", "replace", "backslashreplace", "surrogateescape"]
4343
| None
4444
) = ...,
45+
lines: bool,
46+
chunksize: int,
47+
compression: CompressionOptions = ...,
48+
nrows: int | None = ...,
49+
storage_options: StorageOptions = ...,
50+
dtype_backend: DtypeBackend | NoDefault = ...,
51+
engine: Literal["ujson"] = ...,
52+
) -> JsonReader[Series]: ...
53+
@overload
54+
def read_json(
55+
path_or_buf: FilePath | ReadBuffer[bytes],
56+
*,
57+
orient: JsonSeriesOrient | None = ...,
58+
typ: Literal["series"],
59+
dtype: bool | Mapping[HashableT, DtypeArg] | None = ...,
60+
convert_axes: bool | None = ...,
61+
convert_dates: bool | list[str] = ...,
62+
keep_default_dates: bool = ...,
63+
precise_float: bool = ...,
64+
date_unit: TimeUnit | None = ...,
65+
encoding: str | None = ...,
66+
encoding_errors: (
67+
Literal["strict", "ignore", "replace", "backslashreplace", "surrogateescape"]
68+
| None
69+
) = ...,
4570
lines: Literal[True],
4671
chunksize: int,
4772
compression: CompressionOptions = ...,
4873
nrows: int | None = ...,
4974
storage_options: StorageOptions = ...,
5075
dtype_backend: DtypeBackend | NoDefault = ...,
76+
engine: Literal["pyarrow"] = ...,
5177
) -> JsonReader[Series]: ...
5278
@overload
5379
def read_json(
54-
path_or_buf: FilePath | ReadBuffer[str] | ReadBuffer[bytes],
80+
path_or_buf: FilePath | ReadBuffer[bytes],
5581
*,
5682
orient: JsonFrameOrient | None = ...,
5783
typ: Literal["frame"] = ...,
@@ -72,6 +98,7 @@ def read_json(
7298
nrows: int | None = ...,
7399
storage_options: StorageOptions = ...,
74100
dtype_backend: DtypeBackend | NoDefault = ...,
101+
engine: Literal["pyarrow"] = ...,
75102
) -> JsonReader[DataFrame]: ...
76103
@overload
77104
def read_json(
@@ -96,6 +123,32 @@ def read_json(
96123
nrows: int | None = ...,
97124
storage_options: StorageOptions = ...,
98125
dtype_backend: DtypeBackend | NoDefault = ...,
126+
engine: Literal["ujson"] = ...,
127+
) -> Series: ...
128+
@overload
129+
def read_json(
130+
path_or_buf: FilePath | ReadBuffer[bytes],
131+
*,
132+
orient: JsonSeriesOrient | None = ...,
133+
typ: Literal["series"],
134+
dtype: bool | Mapping[HashableT, DtypeArg] | None = ...,
135+
convert_axes: bool | None = ...,
136+
convert_dates: bool | list[str] = ...,
137+
keep_default_dates: bool = ...,
138+
precise_float: bool = ...,
139+
date_unit: TimeUnit | None = ...,
140+
encoding: str | None = ...,
141+
encoding_errors: (
142+
Literal["strict", "ignore", "replace", "backslashreplace", "surrogateescape"]
143+
| None
144+
) = ...,
145+
lines: Literal[True] = ...,
146+
chunksize: None = ...,
147+
compression: CompressionOptions = ...,
148+
nrows: int | None = ...,
149+
storage_options: StorageOptions = ...,
150+
dtype_backend: DtypeBackend | NoDefault = ...,
151+
engine: Literal["pyarrow"] = ...,
99152
) -> Series: ...
100153
@overload
101154
def read_json(
@@ -120,6 +173,32 @@ def read_json(
120173
nrows: int | None = ...,
121174
storage_options: StorageOptions = ...,
122175
dtype_backend: DtypeBackend | NoDefault = ...,
176+
engine: Literal["ujson"] = ...,
177+
) -> DataFrame: ...
178+
@overload
179+
def read_json(
180+
path_or_buf: FilePath | ReadBuffer[bytes],
181+
*,
182+
orient: JsonFrameOrient | None = ...,
183+
typ: Literal["frame"] = ...,
184+
dtype: bool | Mapping[HashableT, DtypeArg] | None = ...,
185+
convert_axes: bool | None = ...,
186+
convert_dates: bool | list[str] = ...,
187+
keep_default_dates: bool = ...,
188+
precise_float: bool = ...,
189+
date_unit: TimeUnit | None = ...,
190+
encoding: str | None = ...,
191+
encoding_errors: (
192+
Literal["strict", "ignore", "replace", "backslashreplace", "surrogateescape"]
193+
| None
194+
) = ...,
195+
lines: Literal[True] = ...,
196+
chunksize: None = ...,
197+
compression: CompressionOptions = ...,
198+
nrows: int | None = ...,
199+
storage_options: StorageOptions = ...,
200+
dtype_backend: DtypeBackend | NoDefault = ...,
201+
engine: Literal["pyarrow"] = ...,
123202
) -> DataFrame: ...
124203

125204
class JsonReader(abc.Iterator, Generic[NDFrameT]):

tests/test_io.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,3 +1625,28 @@ def test_read_excel_index_col() -> None:
16251625
),
16261626
pd.DataFrame,
16271627
)
1628+
1629+
1630+
def test_read_json_engine() -> None:
1631+
"""Test the engine argument for `pd.read_json` introduced with pandas 2.0."""
1632+
data = """{"index": {"0": 0, "1": 1},
1633+
"a": {"0": 1, "1": null},
1634+
"b": {"0": 2.5, "1": 4.5},
1635+
"c": {"0": true, "1": false},
1636+
"d": {"0": "a", "1": "b"},
1637+
"e": {"0": 1577.2, "1": 1577.1}}"""
1638+
check(
1639+
assert_type(pd.read_json(io.StringIO(data), engine="ujson"), pd.DataFrame),
1640+
pd.DataFrame,
1641+
)
1642+
1643+
data_lines = b"""{"col 1":"a","col 2":"b"}
1644+
{"col 1":"c","col 2":"d"}"""
1645+
dd = io.BytesIO(data_lines)
1646+
check(
1647+
assert_type(
1648+
pd.read_json(dd, lines=True, engine="pyarrow"),
1649+
pd.DataFrame,
1650+
),
1651+
pd.DataFrame,
1652+
)

0 commit comments

Comments
 (0)