Skip to content

Commit 5c18b8a

Browse files
author
tp
committed
make CategoricalIndex.__contains__ compatible with np<1.13
1 parent 14b8ab7 commit 5c18b8a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pandas/core/indexes/category.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,18 @@ def __contains__(self, key):
326326
hash(key)
327327
if isna(key):
328328
return self.isna().any()
329-
elif self.categories._defer_to_indexing: # e.g. Interval values
329+
try:
330330
loc = self.categories.get_loc(key)
331-
return np.isin(self.codes, loc).any()
332-
elif key in self.categories:
333-
return self.categories.get_loc(key) in self._engine
334-
else:
331+
except KeyError:
335332
return False
333+
if is_scalar(loc):
334+
return loc in self._engine
335+
else: # if self.categories is IntervalIndex, loc is an array
336+
return any(loc_ in self._engine for loc_ in loc)
336337

337338
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
338339
def contains(self, key):
340+
hash(key)
339341
return key in self
340342

341343
def __array__(self, dtype=None):

0 commit comments

Comments
 (0)