Skip to content

Commit 93183ba

Browse files
KimDoubleBWillAyd
authored andcommitted
fix unnecessary sort in pd.read_json and orient="index" (#28606)
1 parent 43687c0 commit 93183ba

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ I/O
311311
- Bug in :func:`DataFrame.to_string` where values were truncated using display options instead of outputting the full content (:issue:`9784`)
312312
- Bug in :meth:`DataFrame.to_json` where a datetime column label would not be written out in ISO format with ``orient="table"`` (:issue:`28130`)
313313
- Bug in :func:`DataFrame.to_parquet` where writing to GCS would fail with `engine='fastparquet'` if the file did not already exist (:issue:`28326`)
314+
- Bug in :meth:`DataFrame.read_json` where using ``orient="index"`` would not maintain the order (:issue:`28557`)
314315
- Bug in :meth:`DataFrame.to_html` where the length of the ``formatters`` argument was not verified (:issue:`28469`)
315316

316317
Plotting

pandas/io/json/_json.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from pandas.core.dtypes.common import ensure_str, is_period_dtype
1414

15-
from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
15+
from pandas import DataFrame, MultiIndex, Series, compat, isna, to_datetime
1616
from pandas._typing import Scalar
1717
from pandas.core.reshape.concat import concat
1818

@@ -1112,15 +1112,13 @@ def _parse_no_numpy(self):
11121112
self.check_keys_split(decoded)
11131113
self.obj = DataFrame(dtype=None, **decoded)
11141114
elif orient == "index":
1115-
self.obj = (
1116-
DataFrame.from_dict(
1117-
loads(json, precise_float=self.precise_float),
1118-
dtype=None,
1119-
orient="index",
1120-
)
1121-
.sort_index(axis="columns")
1122-
.sort_index(axis="index")
1115+
self.obj = DataFrame.from_dict(
1116+
loads(json, precise_float=self.precise_float),
1117+
dtype=None,
1118+
orient="index",
11231119
)
1120+
if compat.PY35:
1121+
self.obj = self.obj.sort_index(axis="columns").sort_index(axis="index")
11241122
elif orient == "table":
11251123
self.obj = parse_table_schema(json, precise_float=self.precise_float)
11261124
else:

pandas/tests/io/json/test_pandas.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype):
166166

167167
expected = self.frame.copy()
168168

169-
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
170-
# TODO: debug why sort is required
169+
if not numpy and PY35 and orient in ("index", "columns"):
171170
expected = expected.sort_index()
172171

173172
assert_json_roundtrip_equal(result, expected, orient)
@@ -181,7 +180,7 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
181180
data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype
182181
)
183182
expected = self.intframe.copy()
184-
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
183+
if not numpy and PY35 and orient in ("index", "columns"):
185184
expected = expected.sort_index()
186185

187186
if (
@@ -216,7 +215,7 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
216215
)
217216

218217
expected = df.copy()
219-
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
218+
if not numpy and PY35 and orient in ("index", "columns"):
220219
expected = expected.sort_index()
221220

222221
if not dtype:

0 commit comments

Comments
 (0)