Skip to content

Commit be1a62d

Browse files
jbrockmendeljreback
authored andcommitted
REF: share delete between DTI/TDI/PI (#30695)
1 parent d5fa4f5 commit be1a62d

File tree

3 files changed

+23
-66
lines changed

3 files changed

+23
-66
lines changed

pandas/core/indexes/datetimelike.py

+21
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,27 @@ def shift(self, periods=1, freq=None):
591591
result = self._data._time_shift(periods, freq=freq)
592592
return type(self)(result, name=self.name)
593593

594+
# --------------------------------------------------------------------
595+
# List-like Methods
596+
597+
def delete(self, loc):
598+
new_i8s = np.delete(self.asi8, loc)
599+
600+
freq = None
601+
if is_period_dtype(self):
602+
freq = self.freq
603+
elif is_integer(loc):
604+
if loc in (0, -len(self), -1, len(self) - 1):
605+
freq = self.freq
606+
else:
607+
if is_list_like(loc):
608+
loc = lib.maybe_indices_to_slice(ensure_int64(np.array(loc)), len(self))
609+
if isinstance(loc, slice) and loc.step in (1, None):
610+
if loc.start in (0, None) or loc.stop in (len(self), None):
611+
freq = self.freq
612+
613+
return self._shallow_copy(new_i8s, freq=freq)
614+
594615

595616
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
596617
"""

pandas/core/indexes/datetimes.py

+1-36
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@
99
from pandas._libs.tslibs import ccalendar, fields, parsing, timezones
1010
from pandas.util._decorators import Appender, Substitution, cache_readonly
1111

12-
from pandas.core.dtypes.common import (
13-
_NS_DTYPE,
14-
ensure_int64,
15-
is_float,
16-
is_integer,
17-
is_list_like,
18-
is_scalar,
19-
)
12+
from pandas.core.dtypes.common import _NS_DTYPE, is_float, is_integer, is_scalar
2013
from pandas.core.dtypes.concat import concat_compat
2114
from pandas.core.dtypes.dtypes import DatetimeTZDtype
2215
from pandas.core.dtypes.missing import isna
@@ -1023,34 +1016,6 @@ def insert(self, loc, item):
10231016
return self.astype(object).insert(loc, item)
10241017
raise TypeError("cannot insert DatetimeIndex with incompatible label")
10251018

1026-
def delete(self, loc):
1027-
"""
1028-
Make a new DatetimeIndex with passed location(s) deleted.
1029-
1030-
Parameters
1031-
----------
1032-
loc: int, slice or array of ints
1033-
Indicate which sub-arrays to remove.
1034-
1035-
Returns
1036-
-------
1037-
new_index : DatetimeIndex
1038-
"""
1039-
new_dates = np.delete(self.asi8, loc)
1040-
1041-
freq = None
1042-
if is_integer(loc):
1043-
if loc in (0, -len(self), -1, len(self) - 1):
1044-
freq = self.freq
1045-
else:
1046-
if is_list_like(loc):
1047-
loc = lib.maybe_indices_to_slice(ensure_int64(np.array(loc)), len(self))
1048-
if isinstance(loc, slice) and loc.step in (1, None):
1049-
if loc.start in (0, None) or loc.stop in (len(self), None):
1050-
freq = self.freq
1051-
1052-
return self._shallow_copy(new_dates, freq=freq)
1053-
10541019
def indexer_at_time(self, time, asof=False):
10551020
"""
10561021
Return index locations of index values at particular time of day

pandas/core/indexes/timedeltas.py

+1-30
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
import numpy as np
55

6-
from pandas._libs import NaT, Timedelta, index as libindex, lib
6+
from pandas._libs import NaT, Timedelta, index as libindex
77
from pandas.util._decorators import Appender, Substitution
88

99
from pandas.core.dtypes.common import (
1010
_TD_DTYPE,
11-
ensure_int64,
1211
is_float,
1312
is_integer,
1413
is_list_like,
@@ -476,34 +475,6 @@ def insert(self, loc, item):
476475
return self.astype(object).insert(loc, item)
477476
raise TypeError("cannot insert TimedeltaIndex with incompatible label")
478477

479-
def delete(self, loc):
480-
"""
481-
Make a new TimedeltaIndex with passed location(s) deleted.
482-
483-
Parameters
484-
----------
485-
loc: int, slice or array of ints
486-
Indicate which sub-arrays to remove.
487-
488-
Returns
489-
-------
490-
new_index : TimedeltaIndex
491-
"""
492-
new_tds = np.delete(self.asi8, loc)
493-
494-
freq = None
495-
if is_integer(loc):
496-
if loc in (0, -len(self), -1, len(self) - 1):
497-
freq = self.freq
498-
else:
499-
if is_list_like(loc):
500-
loc = lib.maybe_indices_to_slice(ensure_int64(np.array(loc)), len(self))
501-
if isinstance(loc, slice) and loc.step in (1, None):
502-
if loc.start in (0, None) or loc.stop in (len(self), None):
503-
freq = self.freq
504-
505-
return self._shallow_copy(new_tds, freq=freq)
506-
507478

508479
TimedeltaIndex._add_comparison_ops()
509480
TimedeltaIndex._add_logical_methods_disabled()

0 commit comments

Comments
 (0)