Skip to content

Commit

Permalink
REF: simplify DatetimeEngine.__contains__ (#43702)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Sep 22, 2021
1 parent 556f437 commit 36d5c9b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 6 additions & 13 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,12 @@ cdef class DatetimeEngine(Int64Engine):
def __contains__(self, val: object) -> bool:
# We assume before we get here:
# - val is hashable
cdef:
int64_t loc, conv

conv = self._unbox_scalar(val)
if self.over_size_threshold and self.is_monotonic_increasing:
if not self.is_unique:
return self._get_loc_duplicates(conv)
values = self.values
loc = values.searchsorted(conv, side='left')
return values[loc] == conv

self._ensure_mapping_populated()
return conv in self.mapping
self._unbox_scalar(val)
try:
self.get_loc(val)
return True
except KeyError:
return False

cdef _call_monotonic(self, values):
return algos.is_monotonic(values, timelike=True)
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3953,6 +3953,8 @@ def __delitem__(self, key) -> None:
maybe_shortcut = False
if self.ndim == 2 and isinstance(self.columns, MultiIndex):
try:
# By using engine's __contains__ we effectively
# restrict to same-length tuples
maybe_shortcut = key not in self.columns._engine
except TypeError:
pass
Expand Down

0 comments on commit 36d5c9b

Please sign in to comment.