Skip to content

PERF: Allow RangeIndex.take to return a RangeIndex when possible #57445

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 6 commits into from
Feb 18, 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 @@ -166,6 +166,7 @@ Performance improvements
- Performance improvement in :meth:`Index.take` when ``indices`` is a full range indexer from zero to length of index (:issue:`56806`)
- Performance improvement in :meth:`MultiIndex.equals` for equal length indexes (:issue:`56990`)
- Performance improvement in :meth:`RangeIndex.append` when appending the same index (:issue:`57252`)
- Performance improvement in :meth:`RangeIndex.take` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57445`)
- Performance improvement in indexing operations for string dtypes (:issue:`56997`)
-

Expand Down
13 changes: 7 additions & 6 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,13 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
# Get the stop value from "next" or alternatively
# from the last non-empty index
stop = non_empty_indexes[-1].stop if next_ is None else next_
return RangeIndex(start, stop, step).rename(name)
if len(non_empty_indexes) == 1:
step = non_empty_indexes[0].step
return RangeIndex(start, stop, step, name=name)

# Here all "indexes" had 0 length, i.e. were empty.
# In this case return an empty range index.
return RangeIndex(0, 0).rename(name)
return RangeIndex(_empty_range, name=name)

def __len__(self) -> int:
"""
Expand Down Expand Up @@ -1168,7 +1170,7 @@ def take( # type: ignore[override]
allow_fill: bool = True,
fill_value=None,
**kwargs,
) -> Index:
) -> Self | Index:
if kwargs:
nv.validate_take((), kwargs)
if is_scalar(indices):
Expand All @@ -1179,7 +1181,7 @@ def take( # type: ignore[override]
self._maybe_disallow_fill(allow_fill, fill_value, indices)

if len(indices) == 0:
taken = np.array([], dtype=self.dtype)
return type(self)(_empty_range, name=self.name)
else:
ind_max = indices.max()
if ind_max >= len(self):
Expand All @@ -1199,5 +1201,4 @@ def take( # type: ignore[override]
if self.start != 0:
taken += self.start

# _constructor so RangeIndex-> Index with an int64 dtype
return self._constructor._simple_new(taken, name=self.name)
return self._shallow_copy(taken, name=self.name)
17 changes: 17 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,20 @@ def test_range_index_rsub_by_const(self):
result = 3 - RangeIndex(0, 4, 1)
expected = RangeIndex(3, -1, -1)
tm.assert_index_equal(result, expected)


def test_take_return_rangeindex():
ri = RangeIndex(5, name="foo")
result = ri.take([])
expected = RangeIndex(0, name="foo")
tm.assert_index_equal(result, expected, exact=True)

result = ri.take([3, 4])
expected = RangeIndex(3, 5, name="foo")
tm.assert_index_equal(result, expected, exact=True)


def test_append_one_nonempty_preserve_step():
expected = RangeIndex(0, -1, -1)
result = RangeIndex(0).append([expected])
tm.assert_index_equal(result, expected, exact=True)