Skip to content

Commit 36d5c9b

Browse files
authored
REF: simplify DatetimeEngine.__contains__ (#43702)
1 parent 556f437 commit 36d5c9b

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

pandas/_libs/index.pyx

+6-13
Original file line numberDiff line numberDiff line change
@@ -418,19 +418,12 @@ cdef class DatetimeEngine(Int64Engine):
418418
def __contains__(self, val: object) -> bool:
419419
# We assume before we get here:
420420
# - val is hashable
421-
cdef:
422-
int64_t loc, conv
423-
424-
conv = self._unbox_scalar(val)
425-
if self.over_size_threshold and self.is_monotonic_increasing:
426-
if not self.is_unique:
427-
return self._get_loc_duplicates(conv)
428-
values = self.values
429-
loc = values.searchsorted(conv, side='left')
430-
return values[loc] == conv
431-
432-
self._ensure_mapping_populated()
433-
return conv in self.mapping
421+
self._unbox_scalar(val)
422+
try:
423+
self.get_loc(val)
424+
return True
425+
except KeyError:
426+
return False
434427

435428
cdef _call_monotonic(self, values):
436429
return algos.is_monotonic(values, timelike=True)

pandas/core/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -3953,6 +3953,8 @@ def __delitem__(self, key) -> None:
39533953
maybe_shortcut = False
39543954
if self.ndim == 2 and isinstance(self.columns, MultiIndex):
39553955
try:
3956+
# By using engine's __contains__ we effectively
3957+
# restrict to same-length tuples
39563958
maybe_shortcut = key not in self.columns._engine
39573959
except TypeError:
39583960
pass

0 commit comments

Comments
 (0)