diff --git a/doc/source/whatsnew/v1.4.4.rst b/doc/source/whatsnew/v1.4.4.rst index b462f0c6a8ffe..0f3d7a55fd6d7 100644 --- a/doc/source/whatsnew/v1.4.4.rst +++ b/doc/source/whatsnew/v1.4.4.rst @@ -14,7 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index 634187a317baf..71ecb358cf575 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -157,7 +157,7 @@ def _get_combined_index( index = union_indexes(indexes, sort=False) index = ensure_index(index) - if sort: + if sort and not index.is_monotonic_increasing: index = safe_sort_index(index) # GH 29879 if copy: diff --git a/pandas/tests/reshape/concat/test_index.py b/pandas/tests/reshape/concat/test_index.py index b20e4bcc2256b..74a3e93c32ebe 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -387,3 +387,14 @@ def test_concat_with_levels_with_none_keys(self, levels): msg = "levels supported only when keys is not None" with pytest.raises(ValueError, match=msg): concat([df1, df2], levels=levels) + + def test_concat_range_index_result(self): + # GH#47501 + df1 = DataFrame({"a": [1, 2]}) + df2 = DataFrame({"b": [1, 2]}) + + result = concat([df1, df2], sort=True, axis=1) + expected = DataFrame({"a": [1, 2], "b": [1, 2]}) + tm.assert_frame_equal(result, expected) + expected_index = pd.RangeIndex(0, 2) + tm.assert_index_equal(result.index, expected_index, exact=True)