Skip to content

Commit cef3d80

Browse files
committed
PERF: 5x speedup for read_json() with orient='index' by avoiding transpose
1 parent c64c9cb commit cef3d80

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ Performance improvements
939939
- Improved performance by removing the need for a garbage collect when checking for ``SettingWithCopyWarning`` (:issue:`27031`)
940940
- For :meth:`to_datetime` changed default value of cache parameter to ``True`` (:issue:`26043`)
941941
- Improved performance of :class:`DatetimeIndex` and :class:`PeriodIndex` slicing given non-unique, monotonic data (:issue:`27136`).
942+
- Improved performance of :meth:`pd.read_json` for index-oriented data. (:issue:`26773`)
942943
943944
944945
.. _whatsnew_0250.bug_fixes:

pandas/io/json/_json.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,9 +1085,15 @@ def _parse_no_numpy(self):
10851085
self.check_keys_split(decoded)
10861086
self.obj = DataFrame(dtype=None, **decoded)
10871087
elif orient == "index":
1088-
self.obj = DataFrame(
1089-
loads(json, precise_float=self.precise_float), dtype=None
1090-
).T
1088+
self.obj = (
1089+
DataFrame.from_dict(
1090+
loads(json, precise_float=self.precise_float),
1091+
dtype=None,
1092+
orient="index",
1093+
)
1094+
.sort_index(axis="columns")
1095+
.sort_index(axis="index")
1096+
)
10911097
elif orient == "table":
10921098
self.obj = parse_table_schema(json, precise_float=self.precise_float)
10931099
else:

0 commit comments

Comments
 (0)