Skip to content

Commit 6406840

Browse files
committed
Update _json.py
1 parent a4b7f95 commit 6406840

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

pandas/io/json/_json.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
from pandas.core.dtypes.common import (
3333
ensure_str,
3434
is_string_dtype,
35+
pandas_dtype,
3536
)
3637
from pandas.core.dtypes.dtypes import PeriodDtype
3738

3839
from pandas import (
40+
ArrowDtype,
3941
DataFrame,
4042
Index,
4143
MultiIndex,
@@ -953,12 +955,23 @@ def _read_pyarrow(self) -> DataFrame:
953955
Read JSON using the pyarrow engine.
954956
"""
955957
pyarrow_json = import_optional_dependency("pyarrow.json")
956-
957-
pa_table = pyarrow_json.read_json(self.data)
958-
df = arrow_table_to_pandas(pa_table, dtype_backend=self.dtype_backend)
958+
options = None
959959

960960
if isinstance(self.dtype, dict):
961-
df = df.astype(self.dtype)
961+
pa = import_optional_dependency("pyarrow")
962+
fields = []
963+
for field, dtype in self.dtype.items():
964+
pd_dtype = pandas_dtype(dtype)
965+
if isinstance(pd_dtype, ArrowDtype):
966+
fields.append((field, pd_dtype.pyarrow_dtype))
967+
968+
schema = pa.schema(fields)
969+
options = pyarrow_json.ParseOptions(
970+
explicit_schema=schema, unexpected_field_behavior="infer"
971+
)
972+
973+
pa_table = pyarrow_json.read_json(self.data, parse_options=options)
974+
df = arrow_table_to_pandas(pa_table, dtype_backend=self.dtype_backend)
962975

963976
return df
964977

0 commit comments

Comments
 (0)