@@ -324,18 +324,31 @@ def _reverse_indexer(self):
324
324
@Appender (_index_shared_docs ['__contains__' ] % _index_doc_kwargs )
325
325
def __contains__ (self , key ):
326
326
hash (key )
327
- if isna (key ):
327
+
328
+ if isna (key ): # is key NaN?
328
329
return self .isna ().any ()
329
- elif self .categories ._defer_to_indexing : # e.g. Interval values
330
+
331
+ # is key in self.categories? Then get its location
332
+ # if not (i.e. KeyError), it logically can't be in self either
333
+ try :
330
334
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 :
335
+ except KeyError :
335
336
return False
336
337
338
+ # loc is the location of key in self.categories, but also the value
339
+ # for key in self.codes and in self._engine. key may be in categories,
340
+ # but still not in self, check this. Example:
341
+ # 'b' in CategoricalIndex(['a'], categories=['a', 'b']) # False
342
+ if is_scalar (loc ):
343
+ return loc in self ._engine
344
+ else :
345
+ # if self.categories is IntervalIndex, loc is an array
346
+ # check if any scalar of the array is in self._engine
347
+ return any (loc_ in self ._engine for loc_ in loc )
348
+
337
349
@Appender (_index_shared_docs ['contains' ] % _index_doc_kwargs )
338
350
def contains (self , key ):
351
+ hash (key )
339
352
return key in self
340
353
341
354
def __array__ (self , dtype = None ):
0 commit comments