Skip to content

Commit a373e0e

Browse files
qwhelanjreback
authored andcommitted
PERF: 5x speedup for read_json() with orient='index' by avoiding transpose (#26773)
1 parent 0d2ec3e commit a373e0e

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
@@ -980,6 +980,7 @@ Performance improvements
980980
- Improved performance by removing the need for a garbage collect when checking for ``SettingWithCopyWarning`` (:issue:`27031`)
981981
- For :meth:`to_datetime` changed default value of cache parameter to ``True`` (:issue:`26043`)
982982
- Improved performance of :class:`DatetimeIndex` and :class:`PeriodIndex` slicing given non-unique, monotonic data (:issue:`27136`).
983+
- Improved performance of :meth:`pd.read_json` for index-oriented data. (:issue:`26773`)
983984
984985
.. _whatsnew_0250.bug_fixes:
985986

pandas/io/json/_json.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,15 @@ def _parse_no_numpy(self):
10921092
self.check_keys_split(decoded)
10931093
self.obj = DataFrame(dtype=None, **decoded)
10941094
elif orient == "index":
1095-
self.obj = DataFrame(
1096-
loads(json, precise_float=self.precise_float), dtype=None
1097-
).T
1095+
self.obj = (
1096+
DataFrame.from_dict(
1097+
loads(json, precise_float=self.precise_float),
1098+
dtype=None,
1099+
orient="index",
1100+
)
1101+
.sort_index(axis="columns")
1102+
.sort_index(axis="index")
1103+
)
10981104
elif orient == "table":
10991105
self.obj = parse_table_schema(json, precise_float=self.precise_float)
11001106
else:

0 commit comments

Comments
 (0)