Skip to content

PERF: concat([Series, DataFrame], axis=0) returns RangeIndex columns when possible #58119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Performance improvements
- :attr:`Categorical.categories` returns a :class:`RangeIndex` columns instead of an :class:`Index` if the constructed ``values`` was a ``range``. (:issue:`57787`)
- :class:`DataFrame` returns a :class:`RangeIndex` columns when possible when ``data`` is a ``dict`` (:issue:`57943`)
- :class:`Series` returns a :class:`RangeIndex` index when possible when ``data`` is a ``dict`` (:issue:`58118`)
- :func:`concat` returns a :class:`RangeIndex` column when possible when ``objs`` contains :class:`Series` and :class:`DataFrame` and ``axis=0`` (:issue:`58119`)
- :func:`concat` returns a :class:`RangeIndex` level in the :class:`MultiIndex` result when ``keys`` is a ``range`` or :class:`RangeIndex` (:issue:`57542`)
- :meth:`RangeIndex.append` returns a :class:`RangeIndex` instead of a :class:`Index` when appending values that could continue the :class:`RangeIndex` (:issue:`57467`)
- :meth:`Series.str.extract` returns a :class:`RangeIndex` columns instead of an :class:`Index` column when possible (:issue:`57542`)
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,11 @@ def _sanitize_mixed_ndim(
# to have unique names
name = current_column
current_column += 1

obj = sample._constructor({name: obj}, copy=False)
obj = sample._constructor(obj, copy=False)
if isinstance(obj, ABCDataFrame):
obj.columns = range(name, name + 1, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type of Index gets returned by the constructor? Shouldn't we be creating the RangeIndex at a lower level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

._constructor with a dict would have returned an Index column with a single element name. With _constructor, there is currently no way to dictate the return type of the columns (if _constructor supports columns)

else:
obj = sample._constructor({name: obj}, copy=False)

new_objs.append(obj)

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,11 @@ def test_concat_none_with_timezone_timestamp():
result = concat([df1, df2], ignore_index=True)
expected = DataFrame({"A": [None, pd.Timestamp("1990-12-20 00:00:00+00:00")]})
tm.assert_frame_equal(result, expected)


def test_concat_with_series_and_frame_returns_rangeindex_columns():
ser = Series([0])
df = DataFrame([1, 2])
result = concat([ser, df])
expected = DataFrame([0, 1, 2], index=[0, 0, 1])
tm.assert_frame_equal(result, expected, check_column_type=True)