Skip to content

fix unnecessary sort in pd.read_json and orient="index" #28606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 9, 2019
5 changes: 4 additions & 1 deletion pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ def _union_indexes(indexes, sort=True):
if len(indexes) == 1:
result = indexes[0]
if isinstance(result, list):
result = Index(sorted(result))
if sort:
result = Index(sorted(result))
else:
result = Index(result)
return result

indexes, kind = _sanitize_and_check(indexes)
Expand Down
17 changes: 8 additions & 9 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
from pandas import DataFrame, MultiIndex, Series, isna, to_datetime, compat
from pandas._typing import Scalar
from pandas.core.reshape.concat import concat

Expand Down Expand Up @@ -1112,15 +1112,14 @@ def _parse_no_numpy(self):
self.check_keys_split(decoded)
self.obj = DataFrame(dtype=None, **decoded)
elif orient == "index":
self.obj = (
DataFrame.from_dict(
loads(json, precise_float=self.precise_float),
dtype=None,
orient="index",
)
.sort_index(axis="columns")
.sort_index(axis="index")
self.obj = DataFrame.from_dict(
loads(json, precise_float=self.precise_float),
dtype=None,
orient="index",
)
if compat.PY35:
self.obj.sort_index(axis="columns", inplace=True)
self.obj.sort_index(axis="index", inplace=True)
elif orient == "table":
self.obj = parse_table_schema(json, precise_float=self.precise_float)
else:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,10 @@ def test_rename(self, float_frame):
# gets sorted alphabetical
df = DataFrame(data)
renamed = df.rename(index={"foo": "bar", "bar": "foo"})
tm.assert_index_equal(renamed.index, Index(["foo", "bar"]))
tm.assert_index_equal(renamed.index, Index(["bar", "foo"]))

renamed = df.rename(index=str.upper)
tm.assert_index_equal(renamed.index, Index(["BAR", "FOO"]))
tm.assert_index_equal(renamed.index, Index(["FOO", "BAR"]))

# have to pass something
with pytest.raises(TypeError, match="must pass an index to rename"):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_multiindex_setitem(self):
)

expected = df_orig.copy()
expected.iloc[[0, 2, 3]] *= 2
expected.iloc[[0, 1, 3]] *= 2

idx = pd.IndexSlice
df = df_orig.copy()
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype):

expected = self.frame.copy()

if not numpy and (orient == "index" or (PY35 and orient == "columns")):
if not numpy and (PY35 and (orient == "index" or orient == "columns")):
# TODO: debug why sort is required
expected = expected.sort_index()

Expand All @@ -188,7 +188,7 @@ def test_roundtrip_intframe(self, orient, convert_axes, numpy, dtype):
data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype
)
expected = self.intframe.copy()
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
if not numpy and (PY35 and (orient == "index" or orient == "columns")):
expected = expected.sort_index()

if orient == "records" or orient == "values":
Expand Down Expand Up @@ -228,7 +228,7 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
)

expected = df.copy()
if not numpy and (orient == "index" or (PY35 and orient == "columns")):
if not numpy and (PY35 and (orient == "index" or orient == "columns")):
expected = expected.sort_index()

if not dtype:
Expand Down