File tree 4 files changed +28
-13
lines changed
4 files changed +28
-13
lines changed Original file line number Diff line number Diff line change @@ -193,3 +193,17 @@ def time_categorical_series_is_monotonic_increasing(self):
193
193
194
194
def time_categorical_series_is_monotonic_decreasing (self ):
195
195
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
+ ncats = 100
205
+ self .ci = tm .makeCategoricalIndex (N , ncats )
206
+ self .cat = self .ci .categories [0 ]
207
+
208
+ def time_contains (self ):
209
+ self .cat in self .ci
Original file line number Diff line number Diff line change @@ -64,7 +64,9 @@ Performance Improvements
64
64
~~~~~~~~~~~~~~~~~~~~~~~~
65
65
66
66
- Improved performance of :func:`Series.describe` in case of numeric dtpyes (:issue:`21274`)
67
- -
67
+ - Improved performance of membership checks in :class:`CategoricalIndex`
68
+ (i.e. ``x in ci``-style checks are much faster). :meth:`CategoricalIndex.contains`
69
+ is likewise much faster (:issue:`21107`)
68
70
69
71
.. _whatsnew_0240.docs:
70
72
Original file line number Diff line number Diff line change @@ -324,20 +324,19 @@ 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
-
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
332
336
333
337
@Appender (_index_shared_docs ['contains' ] % _index_doc_kwargs )
334
338
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
341
340
342
341
def __array__ (self , dtype = None ):
343
342
""" the array interface, return my values """
Original file line number Diff line number Diff line change @@ -1795,7 +1795,7 @@ def error():
1795
1795
error ()
1796
1796
raise
1797
1797
except :
1798
- error ()
1798
+ raise
1799
1799
1800
1800
def _is_scalar_access (self , key ):
1801
1801
# this is a shortcut accessor to both .loc and .iloc
You can’t perform that action at this time.
0 commit comments