Skip to content

REF: simplify DatetimeEngine.__contains__ #43702

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
Sep 22, 2021
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
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