From e2e2ea53be49fa9c61867ae68152ffd202644fc4 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 5 Mar 2020 16:02:30 -0800 Subject: [PATCH 1/3] ENH: implement ExtensionIndex.insert --- pandas/core/indexes/base.py | 8 +++++--- pandas/core/indexes/extension.py | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6f44b5abf5b04..a351841408e1e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5140,9 +5140,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"): diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index daccb35864e98..41817f9a348c2 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -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 ( @@ -242,6 +243,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) From ca6e5d9ce58303ab06bfd11ee8ccd01b0140a2c5 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 6 Mar 2020 10:36:28 -0800 Subject: [PATCH 2/3] make abstractmethod --- pandas/core/indexes/extension.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 41817f9a348c2..ba509ea977e36 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -1,12 +1,12 @@ """ Shared methods for Index subclasses backed by ExtensionArray. """ +import abc from typing import List 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 ( @@ -243,9 +243,10 @@ def repeat(self, repeats, axis=None): result = self._data.repeat(repeats, axis=axis) return self._shallow_copy(result) + @abc.abstractmethod def insert(self, loc: int, item): # ExtensionIndex subclasses must override Index.insert - raise AbstractMethodError(self) + pass def _concat_same_dtype(self, to_concat, name): arr = type(self._data)._concat_same_type(to_concat) From faf902605145d67ded084677d9b36e5e092458dd Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 12 Mar 2020 08:43:47 -0700 Subject: [PATCH 3/3] revert to AbstractMethodError --- pandas/core/indexes/extension.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 14daa3f85c70c..6d5f0dbb830f9 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -1,12 +1,12 @@ """ Shared methods for Index subclasses backed by ExtensionArray. """ -import abc from typing import List 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 ( @@ -249,10 +249,9 @@ def repeat(self, repeats, axis=None): result = self._data.repeat(repeats, axis=axis) return self._shallow_copy(result) - @abc.abstractmethod def insert(self, loc: int, item): # ExtensionIndex subclasses must override Index.insert - pass + raise AbstractMethodError(self) def _concat_same_dtype(self, to_concat, name): arr = type(self._data)._concat_same_type(to_concat)