File tree Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Expand file tree Collapse file tree 3 files changed +26
-12
lines changed Original file line number Diff line number Diff line change @@ -193,3 +193,16 @@ def time_categorical_series_is_monotonic_increasing(self):
193193
194194 def time_categorical_series_is_monotonic_decreasing (self ):
195195 self .s .is_monotonic_decreasing
196+
197+
198+ class Contains (object ):
199+
200+ goal_time = 0.2
201+
202+ def setup (self ):
203+ N = 10 ** 5
204+ self .ci = tm .makeCategoricalIndex (N )
205+ self .cat = self .ci .categories [0 ]
206+
207+ def time_contains (self ):
208+ self .cat in self .ci
Original file line number Diff line number Diff line change @@ -65,7 +65,9 @@ Performance Improvements
6565~~~~~~~~~~~~~~~~~~~~~~~~
6666
6767- Improved performance of :func:`Series.describe` in case of numeric dtpyes (:issue:`21274`)
68- -
68+ - Improved performance of membership checks in :class:`CategoricalIndex`
69+ (i.e. ``x in ci``-style checks are much faster). :meth:`CategoricalIndex.contains`
70+ is likewise much faster (:issue:`21369`)
6971
7072.. _whatsnew_0240.docs:
7173
Original file line number Diff line number Diff line change @@ -324,20 +324,19 @@ def _reverse_indexer(self):
324324 @Appender (_index_shared_docs ['__contains__' ] % _index_doc_kwargs )
325325 def __contains__ (self , key ):
326326 hash (key )
327-
328- if self .categories ._defer_to_indexing :
329- return key in self .categories
330-
331- return key in self .values
327+ if isna (key ):
328+ return self .isna ().any ()
329+ elif self .categories ._defer_to_indexing : # e.g. Interval values
330+ 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+ return False
332336
333337 @Appender (_index_shared_docs ['contains' ] % _index_doc_kwargs )
334338 def contains (self , key ):
335- hash (key )
336-
337- if self .categories ._defer_to_indexing :
338- return self .categories .contains (key )
339-
340- return key in self .values
339+ return key in self
341340
342341 def __array__ (self , dtype = None ):
343342 """ the array interface, return my values """
You can’t perform that action at this time.
0 commit comments