Skip to content
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

ENH: implement ExtensionIndex.insert #32476

Merged
merged 5 commits into from
Mar 14, 2020
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
8 changes: 5 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5197,9 +5197,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to make an abstractmethod?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this I believe is ideal but any concerns on backward compat? Otherwise lgtm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any concerns on backward compat?

Nothing comes to mind

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gothca. This is non-public right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtensionIndex needs to inherit from abc.ABC in order for the abstractmethod decorator to actually work, right?

In [1]: import abc

In [2]: class Foo: 
   ...:     @abc.abstractmethod 
   ...:     def some_meth(self): 
   ...:         pass 
   ...: 

In [3]: class Bar(Foo): 
   ...:     pass 
   ...: 

In [4]: bar = Bar()

In [5]: class Foo2(abc.ABC): 
   ...:     @abc.abstractmethod 
   ...:     def some_meth(self): 
   ...:         pass 
   ...: 

In [6]: class Bar2(Foo2): 
   ...:     pass 
   ...:

In [7]: bar2 = Bar2()
---------------------------------------------------------------------------
TypeError: Can't instantiate abstract class Bar2 with abstract methods some_meth

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