Skip to content

Commit 98a6b3c

Browse files
authored
ENH: implement ExtensionIndex.insert (#32476)
1 parent 029f707 commit 98a6b3c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pandas/core/indexes/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5195,9 +5195,11 @@ def insert(self, loc: int, item):
51955195
-------
51965196
new_index : Index
51975197
"""
5198-
_self = np.asarray(self)
5199-
item = self._coerce_scalar_to_index(item)._ndarray_values
5200-
idx = np.concatenate((_self[:loc], item, _self[loc:]))
5198+
# Note: this method is overriden by all ExtensionIndex subclasses,
5199+
# so self is never backed by an EA.
5200+
arr = np.asarray(self)
5201+
item = self._coerce_scalar_to_index(item)._values
5202+
idx = np.concatenate((arr[:loc], item, arr[loc:]))
52015203
return self._shallow_copy_with_infer(idx)
52025204

52035205
def drop(self, labels, errors: str_t = "raise"):

pandas/core/indexes/extension.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77

88
from pandas.compat.numpy import function as nv
9+
from pandas.errors import AbstractMethodError
910
from pandas.util._decorators import Appender, cache_readonly
1011

1112
from pandas.core.dtypes.common import (
@@ -248,6 +249,10 @@ def repeat(self, repeats, axis=None):
248249
result = self._data.repeat(repeats, axis=axis)
249250
return self._shallow_copy(result)
250251

252+
def insert(self, loc: int, item):
253+
# ExtensionIndex subclasses must override Index.insert
254+
raise AbstractMethodError(self)
255+
251256
def _concat_same_dtype(self, to_concat, name):
252257
arr = type(self._data)._concat_same_type(to_concat)
253258
return type(self)._simple_new(arr, name=name)

0 commit comments

Comments
 (0)