Skip to content

Commit 32da415

Browse files
committed
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 fbdd06e commit 32da415

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
@@ -156,7 +156,7 @@ def _get_combined_index(
156156
index = union_indexes(indexes, sort=False)
157157
index = ensure_index(index)
158158

159-
if sort:
159+
if sort and not index.is_monotonic_increasing:
160160
try:
161161
array_sorted = safe_sort(index)
162162
array_sorted = cast(np.ndarray, array_sorted)

pandas/tests/reshape/concat/test_index.py

+11
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,14 @@ def test_concat_with_various_multiindex_dtypes(
306306
result_df = concat((df1, df2), axis=1)
307307

308308
tm.assert_frame_equal(expected_df, result_df)
309+
310+
def test_concat_range_index_result(self):
311+
# GH#47501
312+
df1 = DataFrame({"a": [1, 2]})
313+
df2 = DataFrame({"b": [1, 2]})
314+
315+
result = concat([df1, df2], sort=True, axis=1)
316+
expected = DataFrame({"a": [1, 2], "b": [1, 2]})
317+
tm.assert_frame_equal(result, expected)
318+
expected_index = pd.RangeIndex(0, 2)
319+
tm.assert_index_equal(result.index, expected_index, exact=True)

0 commit comments

Comments
 (0)