Skip to content

PERF: Return RangeIndex from RangeIndex.join when possible #57543

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -165,6 +165,7 @@ Removal of prior version deprecations/changes

Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- :meth:`RangeIndex.join` returns a :class:`RangeIndex` instead of a :class:`Index` when appending values that could continue the :class:`RangeIndex` (:issue:`57543`)
- Performance improvement in :meth:`DataFrame.join` for sorted but non-unique indexes (:issue:`56941`)
- Performance improvement in :meth:`DataFrame.join` when left and/or right are non-unique and ``how`` is ``"left"``, ``"right"``, or ``"inner"`` (:issue:`56817`)
- Performance improvement in :meth:`DataFrame.join` with ``how="left"`` or ``how="right"`` and ``sort=True`` (:issue:`56919`)
Expand Down
17 changes: 17 additions & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from pandas._typing import (
Axis,
Dtype,
JoinHow,
NaPosition,
Self,
npt,
Expand Down Expand Up @@ -1016,6 +1017,22 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
# In this case return an empty range index.
return RangeIndex(_empty_range, name=name)

def _wrap_join_result(
self,
joined,
other,
lidx: npt.NDArray[np.intp] | None,
ridx: npt.NDArray[np.intp] | None,
how: JoinHow,
) -> tuple[Self, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
join_index, lidx, ridx = super()._wrap_join_result(
joined, other, lidx, ridx, how
)
maybe_ri = self._shallow_copy(join_index._values)
Copy link
Member

Choose a reason for hiding this comment

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

Can we do this more efficiently based on the inputs?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair. Yeah I think working on implementing RangeIndex._join_monotinic would be worth it so I'll close this one out

if isinstance(maybe_ri, type(self)):
return maybe_ri, lidx, ridx
return join_index, lidx, ridx

def __len__(self) -> int:
"""
return the length of the RangeIndex
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def test_combine_first(self, data):
b = pd.Series(data[2:5], index=[2, 3, 4])
result = a.combine_first(b)
expected = pd.Series(data[:5])
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, expected, check_index_type=True)

@pytest.mark.parametrize("frame", [True, False])
@pytest.mark.parametrize(
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/ranges/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,9 @@ def test_join_self(self, join_type):
index = RangeIndex(start=0, stop=20, step=2)
joined = index.join(index, how=join_type)
assert index is joined


def test_join_overlap_returns_rangeindex():
result = RangeIndex(3).join(RangeIndex(2, 5), how="outer")
expected = RangeIndex(5)
tm.assert_index_equal(result, expected, exact=True)