Skip to content

Commit a0e0571

Browse files
authored
REF: share insert between DTI/TDI/PI (#36439)
1 parent 1f89b64 commit a0e0571

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

pandas/core/indexes/datetimelike.py

+46-36
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,50 @@ def delete(self, loc):
575575
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
576576
return type(self)._simple_new(arr, name=self.name)
577577

578+
def insert(self, loc: int, item):
579+
"""
580+
Make new Index inserting new item at location
581+
582+
Parameters
583+
----------
584+
loc : int
585+
item : object
586+
if not either a Python datetime or a numpy integer-like, returned
587+
Index dtype will be object rather than datetime.
588+
589+
Returns
590+
-------
591+
new_index : Index
592+
"""
593+
item = self._data._validate_insert_value(item)
594+
595+
freq = None
596+
if is_period_dtype(self.dtype):
597+
freq = self.freq
598+
elif self.freq is not None:
599+
# freq can be preserved on edge cases
600+
if self.size:
601+
if item is NaT:
602+
pass
603+
elif (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
604+
freq = self.freq
605+
elif (loc == len(self)) and item - self.freq == self[-1]:
606+
freq = self.freq
607+
else:
608+
# Adding a single item to an empty index may preserve freq
609+
if self.freq.is_on_offset(item):
610+
freq = self.freq
611+
612+
arr = self._data
613+
item = arr._unbox_scalar(item)
614+
item = arr._rebox_native(item)
615+
616+
new_values = np.concatenate([arr._ndarray[:loc], [item], arr._ndarray[loc:]])
617+
new_arr = self._data._from_backing_data(new_values)
618+
new_arr._freq = freq
619+
620+
return type(self)._simple_new(new_arr, name=self.name)
621+
578622
# --------------------------------------------------------------------
579623
# Join/Set Methods
580624

@@ -895,45 +939,11 @@ def _maybe_utc_convert(self, other):
895939
# --------------------------------------------------------------------
896940
# List-Like Methods
897941

942+
@Appender(DatetimeIndexOpsMixin.insert.__doc__)
898943
def insert(self, loc, item):
899-
"""
900-
Make new Index inserting new item at location
901-
902-
Parameters
903-
----------
904-
loc : int
905-
item : object
906-
if not either a Python datetime or a numpy integer-like, returned
907-
Index dtype will be object rather than datetime.
908-
909-
Returns
910-
-------
911-
new_index : Index
912-
"""
913944
if isinstance(item, str):
914945
# TODO: Why are strings special?
915946
# TODO: Should we attempt _scalar_from_string?
916947
return self.astype(object).insert(loc, item)
917948

918-
item = self._data._validate_insert_value(item)
919-
920-
freq = None
921-
# check freq can be preserved on edge cases
922-
if self.freq is not None:
923-
if self.size:
924-
if item is NaT:
925-
pass
926-
elif (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
927-
freq = self.freq
928-
elif (loc == len(self)) and item - self.freq == self[-1]:
929-
freq = self.freq
930-
else:
931-
# Adding a single item to an empty index may preserve freq
932-
if self.freq.is_on_offset(item):
933-
freq = self.freq
934-
935-
item = self._data._unbox_scalar(item)
936-
937-
new_i8s = np.concatenate([self[:loc].asi8, [item], self[loc:].asi8])
938-
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
939-
return type(self)._simple_new(arr, name=self.name)
949+
return DatetimeIndexOpsMixin.insert(self, loc, item)

pandas/core/indexes/period.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,7 @@ def insert(self, loc, item):
436436
if not isinstance(item, Period) or self.freq != item.freq:
437437
return self.astype(object).insert(loc, item)
438438

439-
i8result = np.concatenate(
440-
(self[:loc].asi8, np.array([item.ordinal]), self[loc:].asi8)
441-
)
442-
arr = type(self._data)._simple_new(i8result, dtype=self.dtype)
443-
return type(self)._simple_new(arr, name=self.name)
439+
return DatetimeIndexOpsMixin.insert(self, loc, item)
444440

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

0 commit comments

Comments
 (0)