Skip to content

REF: share searchsorted between DTI/TDI/PI, insert between DTI/TDI #31143

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
Jan 20, 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
69 changes: 68 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
)
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
from pandas.core.dtypes.missing import isna
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna

from pandas.core import algorithms
from pandas.core.accessor import PandasDelegate
from pandas.core.arrays import DatetimeArray, ExtensionArray, TimedeltaArray
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
from pandas.core.base import _shared_docs
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import Index, _index_shared_docs
from pandas.core.indexes.extension import (
Expand Down Expand Up @@ -221,6 +222,18 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
self, indices, axis, allow_fill, fill_value, **kwargs
)

@Appender(_shared_docs["searchsorted"])
def searchsorted(self, value, side="left", sorter=None):
if isinstance(value, str):
raise TypeError(
"searchsorted requires compatible dtype or scalar, "
f"not {type(value).__name__}"
)
if isinstance(value, Index):
value = value._data

return self._data.searchsorted(value, side=side, sorter=sorter)

_can_hold_na = True

_na_value = NaT
Expand Down Expand Up @@ -883,6 +896,60 @@ def _wrap_joined_index(self, joined, other):
kwargs["tz"] = getattr(other, "tz", None)
return self._simple_new(joined, name, **kwargs)

# --------------------------------------------------------------------
# List-Like Methods

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, self._data._recognized_scalars):
item = self._data._scalar_type(item)
elif is_valid_nat_for_dtype(item, self.dtype):
# GH 18295
item = self._na_value
elif is_scalar(item) and isna(item):
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)

freq = None
if isinstance(item, self._data._scalar_type) or item is NaT:
self._data._check_compatible_with(item, setitem=True)

# check freq can be preserved on edge cases
if self.size and self.freq is not None:
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
item = item.asm8

try:
new_i8s = np.concatenate(
(self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
)
return self._shallow_copy(new_i8s, freq=freq)
except (AttributeError, TypeError):

# fall back to object index
if isinstance(item, str):
return self.astype(object).insert(loc, item)
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)


class DatetimelikeDelegateMixin(PandasDelegate):
"""
Expand Down
72 changes: 2 additions & 70 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
tslib as libts,
)
from pandas._libs.tslibs import ccalendar, fields, parsing, timezones
from pandas.util._decorators import Appender, Substitution, cache_readonly
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import _NS_DTYPE, is_float, is_integer, is_scalar
from pandas.core.dtypes.dtypes import DatetimeTZDtype
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
from pandas.core.dtypes.missing import isna

from pandas.core.accessor import delegate_names
from pandas.core.arrays.datetimes import (
DatetimeArray,
tz_to_dtype,
validate_tz_from_dtype,
)
from pandas.core.base import _shared_docs
import pandas.core.common as com
from pandas.core.indexes.base import Index, maybe_extract_name
from pandas.core.indexes.datetimelike import (
Expand Down Expand Up @@ -826,19 +825,6 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):

# --------------------------------------------------------------------

@Substitution(klass="DatetimeIndex")
@Appender(_shared_docs["searchsorted"])
def searchsorted(self, value, side="left", sorter=None):
if isinstance(value, str):
raise TypeError(
"searchsorted requires compatible dtype or scalar, "
f"not {type(value).__name__}"
)
if isinstance(value, Index):
value = value._data

return self._data.searchsorted(value, side=side)

def is_type_compatible(self, typ) -> bool:
return typ == self.inferred_type or typ == "datetime"

Expand All @@ -848,60 +834,6 @@ def inferred_type(self) -> str:
# sure we can't have ambiguous indexing
return "datetime64"

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, self._data._recognized_scalars):
item = self._data._scalar_type(item)
elif is_valid_nat_for_dtype(item, self.dtype):
# GH 18295
item = self._na_value
elif is_scalar(item) and isna(item):
# i.e. timedeltat64("NaT")
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)

freq = None
if isinstance(item, self._data._scalar_type) or item is NaT:
self._data._check_compatible_with(item, setitem=True)

# check freq can be preserved on edge cases
if self.size and self.freq is not None:
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
item = item.asm8

try:
new_i8s = np.concatenate(
(self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
)
return self._shallow_copy(new_i8s, freq=freq)
except (AttributeError, TypeError):

# fall back to object index
if isinstance(item, str):
return self.astype(object).insert(loc, item)
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)

def indexer_at_time(self, time, asof=False):
"""
Return index locations of index values at particular time of day
Expand Down
8 changes: 1 addition & 7 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas._libs.tslibs import NaT, frequencies as libfrequencies, resolution
from pandas._libs.tslibs.parsing import parse_time_string
from pandas._libs.tslibs.period import Period
from pandas.util._decorators import Appender, Substitution, cache_readonly
from pandas.util._decorators import Appender, cache_readonly

from pandas.core.dtypes.common import (
ensure_platform_int,
Expand All @@ -30,7 +30,6 @@
raise_on_incompatible,
validate_dtype_freq,
)
from pandas.core.base import _shared_docs
import pandas.core.common as com
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import (
Expand Down Expand Up @@ -465,11 +464,6 @@ def astype(self, dtype, copy=True, how="start"):
# TODO: should probably raise on `how` here, so we don't ignore it.
return super().astype(dtype, copy=copy)

@Substitution(klass="PeriodIndex")
@Appender(_shared_docs["searchsorted"])
def searchsorted(self, value, side="left", sorter=None):
return self._data.searchsorted(value, side=side, sorter=sorter)

@property
def is_full(self) -> bool:
"""
Expand Down
74 changes: 2 additions & 72 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np

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

from pandas.core.dtypes.common import (
_TD_DTYPE,
Expand All @@ -16,12 +16,11 @@
is_timedelta64_ns_dtype,
pandas_dtype,
)
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
from pandas.core.dtypes.missing import isna

from pandas.core.accessor import delegate_names
from pandas.core.arrays import datetimelike as dtl
from pandas.core.arrays.timedeltas import TimedeltaArray, _is_convertible_to_td
from pandas.core.base import _shared_docs
import pandas.core.common as com
from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name
from pandas.core.indexes.datetimelike import (
Expand Down Expand Up @@ -345,82 +344,13 @@ def _partial_td_slice(self, key):

raise NotImplementedError

@Substitution(klass="TimedeltaIndex")
@Appender(_shared_docs["searchsorted"])
def searchsorted(self, value, side="left", sorter=None):
if isinstance(value, str):
raise TypeError(
"searchsorted requires compatible dtype or scalar, "
f"not {type(value).__name__}"
)
if isinstance(value, Index):
value = value._data

return self._data.searchsorted(value, side=side, sorter=sorter)

def is_type_compatible(self, typ) -> bool:
return typ == self.inferred_type or typ == "timedelta"

@property
def inferred_type(self) -> str:
return "timedelta64"

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
"""
# try to convert if possible
if isinstance(item, self._data._recognized_scalars):
item = self._data._scalar_type(item)
elif is_valid_nat_for_dtype(item, self.dtype):
# GH 18295
item = self._na_value
elif is_scalar(item) and isna(item):
# i.e. datetime64("NaT")
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)

freq = None
if isinstance(item, self._data._scalar_type) or item is NaT:
self._data._check_compatible_with(item, setitem=True)

# check freq can be preserved on edge cases
if self.size and self.freq is not None:
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
item = item.asm8

try:
new_i8s = np.concatenate(
(self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
)
tda = type(self._data)._simple_new(new_i8s, freq=freq)
return self._shallow_copy(tda)
except (AttributeError, TypeError):

# fall back to object index
if isinstance(item, str):
return self.astype(object).insert(loc, item)
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)


TimedeltaIndex._add_logical_methods_disabled()

Expand Down