MCPcopy
hub / github.com/pandas-dev/pandas / insert

Method insert

pandas/core/indexes/range.py:1155–1178  ·  view source on GitHub ↗
(self, loc: int, item)

Source from the content-addressed store, hash-verified

1153 return super().delete(loc)
1154
1155 def insert(self, loc: int, item) -> Index:
1156 if is_integer(item) or is_float(item):
1157 # We can retain RangeIndex is inserting at the beginning or end,
1158 # or right in the middle.
1159 if len(self) == 0 and loc == 0 and is_integer(item):
1160 new_rng = range(item, item + self.step, self.step)
1161 return type(self)._simple_new(new_rng, name=self._name)
1162 elif len(self):
1163 rng = self._range
1164 if loc == 0 and item == self[0] - self.step:
1165 new_rng = range(rng.start - rng.step, rng.stop, rng.step)
1166 return type(self)._simple_new(new_rng, name=self._name)
1167
1168 elif loc == len(self) and item == self[-1] + self.step:
1169 new_rng = range(rng.start, rng.stop + rng.step, rng.step)
1170 return type(self)._simple_new(new_rng, name=self._name)
1171
1172 elif len(self) == 2 and item == self[0] + self.step / 2:
1173 # e.g. inserting 1 into [0, 2]
1174 step = int(self.step / 2)
1175 new_rng = range(self.start, self.stop, step)
1176 return type(self)._simple_new(new_rng, name=self._name)
1177
1178 return super().insert(loc, item)
1179
1180 def _concat(self, indexes: list[Index], name: Hashable) -> Index:
1181 """

Callers 1

test_insert_empty_0_locFunction · 0.95

Calls 1

_simple_newMethod · 0.45

Tested by 1

test_insert_empty_0_locFunction · 0.76