-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
REF: share delete, putmask, insert between ndarray-backed EA indexes #37529
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
Changes from all commits
e90c504
523b757
ff44a64
7a3fdd0
355b95c
31218e4
a960014
6c3eaa5
d231df5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
import pandas.core.indexes.base as ibase | ||
from pandas.core.indexes.base import Index, _index_shared_docs | ||
from pandas.core.indexes.extension import ( | ||
ExtensionIndex, | ||
NDArrayBackedExtensionIndex, | ||
inherit_names, | ||
make_wrapped_arith_op, | ||
) | ||
|
@@ -82,7 +82,7 @@ def wrapper(left, right): | |
cache=True, | ||
) | ||
@inherit_names(["mean", "asi8", "freq", "freqstr"], DatetimeLikeArrayMixin) | ||
class DatetimeIndexOpsMixin(ExtensionIndex): | ||
class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex): | ||
""" | ||
Common ops mixin to support a unified interface datetimelike Index. | ||
""" | ||
|
@@ -191,7 +191,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): | |
|
||
maybe_slice = lib.maybe_indices_to_slice(indices, len(self)) | ||
|
||
result = ExtensionIndex.take( | ||
result = NDArrayBackedExtensionIndex.take( | ||
self, indices, axis, allow_fill, fill_value, **kwargs | ||
) | ||
if isinstance(maybe_slice, slice): | ||
|
@@ -496,17 +496,6 @@ def where(self, cond, other=None): | |
arr = self._data._from_backing_data(result) | ||
return type(self)._simple_new(arr, name=self.name) | ||
|
||
def putmask(self, mask, value): | ||
try: | ||
value = self._data._validate_where_value(value) | ||
except (TypeError, ValueError): | ||
return self.astype(object).putmask(mask, value) | ||
|
||
result = self._data._ndarray.copy() | ||
np.putmask(result, mask, value) | ||
arr = self._data._from_backing_data(result) | ||
return type(self)._simple_new(arr, name=self.name) | ||
|
||
def _summary(self, name=None) -> str: | ||
""" | ||
Return a summarized representation. | ||
|
@@ -575,41 +564,30 @@ def shift(self, periods=1, freq=None): | |
# -------------------------------------------------------------------- | ||
# List-like Methods | ||
|
||
def delete(self, loc): | ||
new_i8s = np.delete(self.asi8, loc) | ||
|
||
def _get_delete_freq(self, loc: int): | ||
""" | ||
Find the `freq` for self.delete(loc). | ||
""" | ||
freq = None | ||
if is_period_dtype(self.dtype): | ||
freq = self.freq | ||
elif is_integer(loc): | ||
if loc in (0, -len(self), -1, len(self) - 1): | ||
freq = self.freq | ||
else: | ||
if is_list_like(loc): | ||
loc = lib.maybe_indices_to_slice( | ||
np.asarray(loc, dtype=np.intp), len(self) | ||
) | ||
if isinstance(loc, slice) and loc.step in (1, None): | ||
if loc.start in (0, None) or loc.stop in (len(self), None): | ||
elif self.freq is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i am not sure that these specific freq routines are really worth it given the extra code. Can't we just re-infer the freq? (sure if we do this constantly its not as performant, but this is not likey a common case). ok for here, but if you can have a look at some point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats a reasonable thought. i recently found a PITA bug in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kk maybe worthile to create an issue to see what implications / address later is. |
||
if is_integer(loc): | ||
if loc in (0, -len(self), -1, len(self) - 1): | ||
freq = self.freq | ||
else: | ||
if is_list_like(loc): | ||
loc = lib.maybe_indices_to_slice( | ||
np.asarray(loc, dtype=np.intp), len(self) | ||
) | ||
if isinstance(loc, slice) and loc.step in (1, None): | ||
if loc.start in (0, None) or loc.stop in (len(self), None): | ||
freq = self.freq | ||
return freq | ||
|
||
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq) | ||
return type(self)._simple_new(arr, name=self.name) | ||
|
||
def insert(self, loc: int, item): | ||
def _get_insert_freq(self, loc, item): | ||
""" | ||
Make new Index inserting new item at location | ||
|
||
Parameters | ||
---------- | ||
loc : int | ||
item : object | ||
if not either a Python datetime or a numpy integer-like, returned | ||
Index dtype will be object rather than datetime. | ||
|
||
Returns | ||
------- | ||
new_index : Index | ||
Find the `freq` for self.insert(loc, item). | ||
""" | ||
value = self._data._validate_insert_value(item) | ||
item = self._data._box_func(value) | ||
|
@@ -630,14 +608,20 @@ def insert(self, loc: int, item): | |
# Adding a single item to an empty index may preserve freq | ||
if self.freq.is_on_offset(item): | ||
freq = self.freq | ||
return freq | ||
|
||
arr = self._data | ||
@doc(NDArrayBackedExtensionIndex.delete) | ||
def delete(self, loc): | ||
result = super().delete(loc) | ||
result._data._freq = self._get_delete_freq(loc) | ||
return result | ||
|
||
new_values = np.concatenate([arr._ndarray[:loc], [value], arr._ndarray[loc:]]) | ||
new_arr = self._data._from_backing_data(new_values) | ||
new_arr._freq = freq | ||
@doc(NDArrayBackedExtensionIndex.insert) | ||
def insert(self, loc: int, item): | ||
result = super().insert(loc, item) | ||
|
||
return type(self)._simple_new(new_arr, name=self.name) | ||
result._data._freq = self._get_insert_freq(loc, item) | ||
return result | ||
|
||
# -------------------------------------------------------------------- | ||
# Join/Set Methods | ||
|
Uh oh!
There was an error while loading. Please reload this page.