Skip to content

Commit 05ab1af

Browse files
authored
PERF: concat([Series, DataFrame], axis=0) returns RangeIndex columns when possible (#58119)
* PERF: concat([Series, DataFrame], axis=0) returns RangeIndex columns when possible * Add whatsnew number
1 parent a70ad6f commit 05ab1af

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Performance improvements
298298
- :attr:`Categorical.categories` returns a :class:`RangeIndex` columns instead of an :class:`Index` if the constructed ``values`` was a ``range``. (:issue:`57787`)
299299
- :class:`DataFrame` returns a :class:`RangeIndex` columns when possible when ``data`` is a ``dict`` (:issue:`57943`)
300300
- :class:`Series` returns a :class:`RangeIndex` index when possible when ``data`` is a ``dict`` (:issue:`58118`)
301+
- :func:`concat` returns a :class:`RangeIndex` column when possible when ``objs`` contains :class:`Series` and :class:`DataFrame` and ``axis=0`` (:issue:`58119`)
301302
- :func:`concat` returns a :class:`RangeIndex` level in the :class:`MultiIndex` result when ``keys`` is a ``range`` or :class:`RangeIndex` (:issue:`57542`)
302303
- :meth:`RangeIndex.append` returns a :class:`RangeIndex` instead of a :class:`Index` when appending values that could continue the :class:`RangeIndex` (:issue:`57467`)
303304
- :meth:`Series.str.extract` returns a :class:`RangeIndex` columns instead of an :class:`Index` column when possible (:issue:`57542`)

pandas/core/reshape/concat.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,11 @@ def _sanitize_mixed_ndim(
518518
# to have unique names
519519
name = current_column
520520
current_column += 1
521-
522-
obj = sample._constructor({name: obj}, copy=False)
521+
obj = sample._constructor(obj, copy=False)
522+
if isinstance(obj, ABCDataFrame):
523+
obj.columns = range(name, name + 1, 1)
524+
else:
525+
obj = sample._constructor({name: obj}, copy=False)
523526

524527
new_objs.append(obj)
525528

pandas/tests/reshape/concat/test_concat.py

+8
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,11 @@ def test_concat_none_with_timezone_timestamp():
912912
result = concat([df1, df2], ignore_index=True)
913913
expected = DataFrame({"A": [None, pd.Timestamp("1990-12-20 00:00:00+00:00")]})
914914
tm.assert_frame_equal(result, expected)
915+
916+
917+
def test_concat_with_series_and_frame_returns_rangeindex_columns():
918+
ser = Series([0])
919+
df = DataFrame([1, 2])
920+
result = concat([ser, df])
921+
expected = DataFrame([0, 1, 2], index=[0, 0, 1])
922+
tm.assert_frame_equal(result, expected, check_column_type=True)

0 commit comments

Comments
 (0)