From ec3c66343ed3779a7fcb2c0bd1d1fcaea402f353 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 25 Jun 2022 17:24:36 +0200 Subject: [PATCH 1/3] REGR: concat materializing index even if sort is not necessary --- doc/source/whatsnew/v1.4.4.rst | 2 +- pandas/core/indexes/api.py | 2 +- pandas/tests/reshape/concat/test_index.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) 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..c15231763653c 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -387,3 +387,12 @@ 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) From e12ada3547e484d52e63bd309dcb005a05400ab2 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Tue, 28 Jun 2022 16:12:21 +0200 Subject: [PATCH 2/3] Add check for index --- pandas/tests/reshape/concat/test_index.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/reshape/concat/test_index.py b/pandas/tests/reshape/concat/test_index.py index c15231763653c..2f3bf91e3bdb2 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -396,3 +396,5 @@ def test_concat_range_index_result(self): 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) From 885d19a5e437444e6a1ef9bf91000517de3bd109 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Tue, 28 Jun 2022 20:26:32 +0200 Subject: [PATCH 3/3] Add exact --- pandas/tests/reshape/concat/test_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/concat/test_index.py b/pandas/tests/reshape/concat/test_index.py index 2f3bf91e3bdb2..74a3e93c32ebe 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -397,4 +397,4 @@ def test_concat_range_index_result(self): 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) + tm.assert_index_equal(result.index, expected_index, exact=True)