Skip to content

REF: share delete between DTI/TDI/PI #30695

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 2 commits into from
Jan 4, 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
21 changes: 21 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,27 @@ def shift(self, periods=1, freq=None):
result = self._data._time_shift(periods, freq=freq)
return type(self)(result, name=self.name)

# --------------------------------------------------------------------
# List-like Methods

def delete(self, loc):
Copy link
Contributor

Choose a reason for hiding this comment

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

type if you can

new_i8s = np.delete(self.asi8, loc)

freq = None
if is_period_dtype(self):
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(ensure_int64(np.array(loc)), 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 self._shallow_copy(new_i8s, freq=freq)


class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
"""
Expand Down
37 changes: 1 addition & 36 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
from pandas._libs.tslibs import ccalendar, fields, parsing, timezones
from pandas.util._decorators import Appender, Substitution, cache_readonly

from pandas.core.dtypes.common import (
_NS_DTYPE,
ensure_int64,
is_float,
is_integer,
is_list_like,
is_scalar,
)
from pandas.core.dtypes.common import _NS_DTYPE, is_float, is_integer, is_scalar
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.dtypes import DatetimeTZDtype
from pandas.core.dtypes.missing import isna
Expand Down Expand Up @@ -1031,34 +1024,6 @@ def insert(self, loc, item):
return self.astype(object).insert(loc, item)
raise TypeError("cannot insert DatetimeIndex with incompatible label")

def delete(self, loc):
"""
Make a new DatetimeIndex with passed location(s) deleted.

Parameters
----------
loc: int, slice or array of ints
Indicate which sub-arrays to remove.

Returns
-------
new_index : DatetimeIndex
"""
new_dates = np.delete(self.asi8, loc)

freq = None
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(ensure_int64(np.array(loc)), 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 self._shallow_copy(new_dates, freq=freq)

def indexer_at_time(self, time, asof=False):
"""
Return index locations of index values at particular time of day
Expand Down
31 changes: 1 addition & 30 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

import numpy as np

from pandas._libs import NaT, Timedelta, index as libindex, lib
from pandas._libs import NaT, Timedelta, index as libindex
from pandas.util._decorators import Appender, Substitution

from pandas.core.dtypes.common import (
_TD_DTYPE,
ensure_int64,
is_float,
is_integer,
is_list_like,
Expand Down Expand Up @@ -478,34 +477,6 @@ def insert(self, loc, item):
return self.astype(object).insert(loc, item)
raise TypeError("cannot insert TimedeltaIndex with incompatible label")

def delete(self, loc):
"""
Make a new TimedeltaIndex with passed location(s) deleted.

Parameters
----------
loc: int, slice or array of ints
Indicate which sub-arrays to remove.

Returns
-------
new_index : TimedeltaIndex
"""
new_tds = np.delete(self.asi8, loc)

freq = None
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(ensure_int64(np.array(loc)), 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 self._shallow_copy(new_tds, freq=freq)


TimedeltaIndex._add_comparison_ops()
TimedeltaIndex._add_logical_methods_disabled()
Expand Down