Skip to content

Commit

Permalink
ENH: implement ExtensionIndex.insert (pandas-dev#32476)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and SeeminSyed committed Mar 22, 2020
1 parent 0f94743 commit 402fdfb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5195,9 +5195,11 @@ def insert(self, loc: int, item):
-------
new_index : Index
"""
_self = np.asarray(self)
item = self._coerce_scalar_to_index(item)._ndarray_values
idx = np.concatenate((_self[:loc], item, _self[loc:]))
# Note: this method is overriden by all ExtensionIndex subclasses,
# so self is never backed by an EA.
arr = np.asarray(self)
item = self._coerce_scalar_to_index(item)._values
idx = np.concatenate((arr[:loc], item, arr[loc:]))
return self._shallow_copy_with_infer(idx)

def drop(self, labels, errors: str_t = "raise"):
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np

from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, cache_readonly

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

def insert(self, loc: int, item):
# ExtensionIndex subclasses must override Index.insert
raise AbstractMethodError(self)

def _concat_same_dtype(self, to_concat, name):
arr = type(self._data)._concat_same_type(to_concat)
return type(self)._simple_new(arr, name=name)
Expand Down

0 comments on commit 402fdfb

Please sign in to comment.