Skip to content

PERF: RangeIndex.append returns a RangeIndex when possible #57467

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 10 commits into from
Mar 6, 2024
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 @@ -245,6 +245,7 @@ Removal of prior version deprecations/changes

Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- :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`)
- Performance improvement in :class:`DataFrame` when ``data`` is a ``dict`` and ``columns`` is specified (:issue:`24368`)
- Performance improvement in :meth:`DataFrame.join` for sorted but non-unique indexes (:issue:`56941`)
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,10 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Index([0,1,2,4,5], dtype='int64')
"""
if not all(isinstance(x, RangeIndex) for x in indexes):
return super()._concat(indexes, name)
result = super()._concat(indexes, name)
if result.dtype.kind == "i":
return self._shallow_copy(result._values)
return result

elif len(indexes) == 1:
return indexes[0]
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,20 @@ def test_range_index_rsub_by_const(self):
tm.assert_index_equal(result, expected)


def test_append_non_rangeindex_return_rangeindex():
ri = RangeIndex(1)
result = ri.append(Index([1]))
expected = RangeIndex(2)
Comment on lines +612 to +614
Copy link
Member

Choose a reason for hiding this comment

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

If you think it can be useful to also add a test for the case where it can't return a RangeIndex but an Index maybe we can add it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea; I think that would be useful. Added a test in the latest commit

tm.assert_index_equal(result, expected, exact=True)


def test_append_non_rangeindex_return_index():
ri = RangeIndex(1)
result = ri.append(Index([1, 3, 4]))
expected = Index([0, 1, 3, 4])
tm.assert_index_equal(result, expected, exact=True)


def test_reindex_returns_rangeindex():
ri = RangeIndex(2, name="foo")
result, result_indexer = ri.reindex([1, 2, 3])
Expand Down