Skip to content

PERF: RangeIndex.insert maintains RangeIndex when empty #57833

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 7 commits into from
Mar 16, 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 @@ -275,6 +275,7 @@ Performance improvements
- Performance improvement in :meth:`RangeIndex.__getitem__` with a boolean mask or integers returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57588`)
- Performance improvement in :meth:`RangeIndex.append` when appending the same index (:issue:`57252`)
- Performance improvement in :meth:`RangeIndex.argmin` and :meth:`RangeIndex.argmax` (:issue:`57823`)
- Performance improvement in :meth:`RangeIndex.insert` returning a :class:`RangeIndex` instead of a :class:`Index` when the :class:`RangeIndex` is empty. (:issue:`57833`)
- Performance improvement in :meth:`RangeIndex.round` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57824`)
- Performance improvement in :meth:`RangeIndex.join` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57651`, :issue:`57752`)
- Performance improvement in :meth:`RangeIndex.reindex` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57647`, :issue:`57752`)
Expand Down
34 changes: 19 additions & 15 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def __contains__(self, key: Any) -> bool:
hash(key)
try:
key = ensure_python_int(key)
except TypeError:
except (TypeError, OverflowError):
return False
return key in self._range

Expand Down Expand Up @@ -1009,23 +1009,27 @@ def delete(self, loc) -> Index: # type: ignore[override]
return super().delete(loc)

def insert(self, loc: int, item) -> Index:
if len(self) and (is_integer(item) or is_float(item)):
if is_integer(item) or is_float(item):
# We can retain RangeIndex is inserting at the beginning or end,
# or right in the middle.
rng = self._range
if loc == 0 and item == self[0] - self.step:
new_rng = range(rng.start - rng.step, rng.stop, rng.step)
return type(self)._simple_new(new_rng, name=self._name)

elif loc == len(self) and item == self[-1] + self.step:
new_rng = range(rng.start, rng.stop + rng.step, rng.step)
return type(self)._simple_new(new_rng, name=self._name)

elif len(self) == 2 and item == self[0] + self.step / 2:
# e.g. inserting 1 into [0, 2]
step = int(self.step / 2)
new_rng = range(self.start, self.stop, step)
if len(self) == 0 and loc == 0 and is_integer(item):
new_rng = range(item, item + self.step, self.step)
return type(self)._simple_new(new_rng, name=self._name)
elif len(self):
rng = self._range
if loc == 0 and item == self[0] - self.step:
new_rng = range(rng.start - rng.step, rng.stop, rng.step)
return type(self)._simple_new(new_rng, name=self._name)

elif loc == len(self) and item == self[-1] + self.step:
new_rng = range(rng.start, rng.stop + rng.step, rng.step)
return type(self)._simple_new(new_rng, name=self._name)

elif len(self) == 2 and item == self[0] + self.step / 2:
# e.g. inserting 1 into [0, 2]
step = int(self.step / 2)
new_rng = range(self.start, self.stop, step)
return type(self)._simple_new(new_rng, name=self._name)

return super().insert(loc, item)

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,13 @@ def test_reindex_empty_returns_rangeindex():
tm.assert_numpy_array_equal(result_indexer, expected_indexer)


def test_insert_empty_0_loc():
ri = RangeIndex(0, step=10, name="foo")
result = ri.insert(0, 5)
expected = RangeIndex(5, 15, 10, name="foo")
tm.assert_index_equal(result, expected, exact=True)


def test_append_non_rangeindex_return_rangeindex():
ri = RangeIndex(1)
result = ri.append(Index([1]))
Expand Down
Loading