Skip to content

CLN: fix wrong types getting passed to TDI._get_string_slice #30874

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 4 commits 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
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
duplicated="np.ndarray",
)
_index_shared_docs = dict()
str_t = str


def _make_comparison_op(op, cls):
Expand Down Expand Up @@ -4630,7 +4631,7 @@ def isin(self, values, level=None):
self._validate_index_level(level)
return algos.isin(self, values)

def _get_string_slice(self, key, use_lhs=True, use_rhs=True):
def _get_string_slice(self, key: str_t, use_lhs: bool = True, use_rhs: bool = True):
# this is for partial string indexing,
# overridden in DatetimeIndex, TimedeltaIndex and PeriodIndex
raise NotImplementedError
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ def _parsed_string_to_bounds(self, reso, parsed):
raise KeyError(reso)
return (t1.asfreq(self.freq, how="start"), t1.asfreq(self.freq, how="end"))

def _get_string_slice(self, key):
def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
# TODO: Check for non-True use_lhs/use_rhs
if not self.is_monotonic:
raise ValueError("Partial indexing only valid for ordered time series")

Expand Down
53 changes: 13 additions & 40 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,18 @@ def get_value(self, series, key):
know what you're doing
"""

if _is_convertible_to_td(key):
if isinstance(key, str):
try:
key = Timedelta(key)
except ValueError:
raise KeyError(key)

if isinstance(key, self._data._recognized_scalars) or key is NaT:
key = Timedelta(key)
return self.get_value_maybe_box(series, key)

try:
value = Index.get_value(self, series, key)
except KeyError:
try:
loc = self._get_string_slice(key)
return series[loc]
except (TypeError, ValueError, KeyError):
pass

try:
return self.get_value_maybe_box(series, key)
except (TypeError, ValueError, KeyError):
raise KeyError(key)
else:
return com.maybe_box(self, value, series, key)
value = Index.get_value(self, series, key)
return com.maybe_box(self, value, series, key)

def get_value_maybe_box(self, series, key: Timedelta):
values = self._engine.get_value(com.values_from_object(series), key)
Expand Down Expand Up @@ -288,19 +281,7 @@ def get_loc(self, key, method=None, tolerance=None):
key = Timedelta(key)
return Index.get_loc(self, key, method, tolerance)

try:
return Index.get_loc(self, key, method, tolerance)
except (KeyError, ValueError, TypeError):
try:
return self._get_string_slice(key)
except (TypeError, KeyError, ValueError):
pass

try:
stamp = Timedelta(key)
return Index.get_loc(self, stamp, method, tolerance)
except (KeyError, ValueError):
raise KeyError(key)
return Index.get_loc(self, key, method, tolerance)

def _maybe_cast_slice_bound(self, label, side, kind):
"""
Expand Down Expand Up @@ -330,18 +311,10 @@ def _maybe_cast_slice_bound(self, label, side, kind):

return label

def _get_string_slice(self, key):
if is_integer(key) or is_float(key) or key is NaT:
self._invalid_indexer("slice", key)
loc = self._partial_td_slice(key)
return loc

def _partial_td_slice(self, key):

def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
# TODO: Check for non-True use_lhs/use_rhs
assert isinstance(key, str), type(key)
# given a key, try to figure out a location for a partial slice
if not isinstance(key, str):
return key

raise NotImplementedError

@Substitution(klass="TimedeltaIndex")
Expand Down