Skip to content

REF: share insert between DTI/TDI/PI #36439

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

Merged
merged 1 commit into from
Sep 18, 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
82 changes: 46 additions & 36 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,50 @@ def delete(self, loc):
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):
"""
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
"""
item = self._data._validate_insert_value(item)

freq = None
if is_period_dtype(self.dtype):
freq = self.freq
elif self.freq is not None:
# freq can be preserved on edge cases
if self.size:
if item is NaT:
pass
elif (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
freq = self.freq
elif (loc == len(self)) and item - self.freq == self[-1]:
freq = self.freq
else:
# Adding a single item to an empty index may preserve freq
if self.freq.is_on_offset(item):
freq = self.freq

arr = self._data
item = arr._unbox_scalar(item)
item = arr._rebox_native(item)

new_values = np.concatenate([arr._ndarray[:loc], [item], arr._ndarray[loc:]])
new_arr = self._data._from_backing_data(new_values)
new_arr._freq = freq

return type(self)._simple_new(new_arr, name=self.name)

# --------------------------------------------------------------------
# Join/Set Methods

Expand Down Expand Up @@ -895,45 +939,11 @@ def _maybe_utc_convert(self, other):
# --------------------------------------------------------------------
# List-Like Methods

@Appender(DatetimeIndexOpsMixin.insert.__doc__)
def insert(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
"""
if isinstance(item, str):
# TODO: Why are strings special?
# TODO: Should we attempt _scalar_from_string?
return self.astype(object).insert(loc, item)

item = self._data._validate_insert_value(item)

freq = None
# check freq can be preserved on edge cases
if self.freq is not None:
if self.size:
if item is NaT:
pass
elif (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
freq = self.freq
elif (loc == len(self)) and item - self.freq == self[-1]:
freq = self.freq
else:
# Adding a single item to an empty index may preserve freq
if self.freq.is_on_offset(item):
freq = self.freq

item = self._data._unbox_scalar(item)

new_i8s = np.concatenate([self[:loc].asi8, [item], self[loc:].asi8])
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
return type(self)._simple_new(arr, name=self.name)
return DatetimeIndexOpsMixin.insert(self, loc, item)
Copy link
Member

Choose a reason for hiding this comment

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

Would super().insert(loc, item) be better, more independent on the class name? Here and inPeriodIndex.

Copy link
Contributor

Choose a reason for hiding this comment

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

super() is non-idiomatic here because of the somewhat complex inheitance chain we have; meaning you have to really undertand the mro as a reader; this is simpler. We don't actualy use super() except in a few small cases (maybe should change those).

6 changes: 1 addition & 5 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,7 @@ def insert(self, loc, item):
if not isinstance(item, Period) or self.freq != item.freq:
return self.astype(object).insert(loc, item)

i8result = np.concatenate(
(self[:loc].asi8, np.array([item.ordinal]), self[loc:].asi8)
)
arr = type(self._data)._simple_new(i8result, dtype=self.dtype)
return type(self)._simple_new(arr, name=self.name)
return DatetimeIndexOpsMixin.insert(self, loc, item)

def join(self, other, how="left", level=None, return_indexers=False, sort=False):
"""
Expand Down