Skip to content

Commit 208e4dd

Browse files
phoflyehoshuadimarsky
authored andcommitted
REGR: concat materializing index even if sort is not necessary (pandas-dev#47508)
* REGR: concat materializing index even if sort is not necessary * Add check for index * Add exact
1 parent b9abb7c commit 208e4dd

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v1.4.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17-
-
17+
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
1818
-
1919

2020
.. ---------------------------------------------------------------------------

pandas/core/indexes/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _get_combined_index(
157157
index = union_indexes(indexes, sort=False)
158158
index = ensure_index(index)
159159

160-
if sort:
160+
if sort and not index.is_monotonic_increasing:
161161
index = safe_sort_index(index)
162162
# GH 29879
163163
if copy:

pandas/tests/reshape/concat/test_index.py

+11
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,14 @@ def test_concat_with_levels_with_none_keys(self, levels):
387387
msg = "levels supported only when keys is not None"
388388
with pytest.raises(ValueError, match=msg):
389389
concat([df1, df2], levels=levels)
390+
391+
def test_concat_range_index_result(self):
392+
# GH#47501
393+
df1 = DataFrame({"a": [1, 2]})
394+
df2 = DataFrame({"b": [1, 2]})
395+
396+
result = concat([df1, df2], sort=True, axis=1)
397+
expected = DataFrame({"a": [1, 2], "b": [1, 2]})
398+
tm.assert_frame_equal(result, expected)
399+
expected_index = pd.RangeIndex(0, 2)
400+
tm.assert_index_equal(result.index, expected_index, exact=True)

0 commit comments

Comments
 (0)