|
24 | 24 | import pandas.core.common as com |
25 | 25 | import pandas.core.missing as missing |
26 | 26 | import pandas.core.indexes.base as ibase |
| 27 | +from pandas.core.arrays import Categorical |
27 | 28 |
|
28 | 29 | _index_doc_kwargs = dict(ibase._index_doc_kwargs) |
29 | 30 | _index_doc_kwargs.update(dict(target_klass='CategoricalIndex')) |
@@ -125,7 +126,6 @@ def _create_from_codes(self, codes, categories=None, ordered=None, |
125 | 126 | CategoricalIndex |
126 | 127 | """ |
127 | 128 |
|
128 | | - from pandas.core.arrays import Categorical |
129 | 129 | if categories is None: |
130 | 130 | categories = self.categories |
131 | 131 | if ordered is None: |
@@ -162,7 +162,6 @@ def _create_categorical(self, data, categories=None, ordered=None, |
162 | 162 | if not isinstance(data, ABCCategorical): |
163 | 163 | if ordered is None and dtype is None: |
164 | 164 | ordered = False |
165 | | - from pandas.core.arrays import Categorical |
166 | 165 | data = Categorical(data, categories=categories, ordered=ordered, |
167 | 166 | dtype=dtype) |
168 | 167 | else: |
@@ -328,23 +327,8 @@ def __contains__(self, key): |
328 | 327 | if isna(key): # if key is a NaN, check if any NaN is in self. |
329 | 328 | return self.hasnans |
330 | 329 |
|
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: |
334 | | - loc = self.categories.get_loc(key) |
335 | | - except KeyError: |
336 | | - return False |
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) |
| 330 | + return Categorical._contains(key, categories=self.categories, |
| 331 | + container=self._engine) |
348 | 332 |
|
349 | 333 | @Appender(_index_shared_docs['contains'] % _index_doc_kwargs) |
350 | 334 | def contains(self, key): |
@@ -479,7 +463,6 @@ def where(self, cond, other=None): |
479 | 463 | other = self._na_value |
480 | 464 | values = np.where(cond, self.values, other) |
481 | 465 |
|
482 | | - from pandas.core.arrays import Categorical |
483 | 466 | cat = Categorical(values, |
484 | 467 | categories=self.categories, |
485 | 468 | ordered=self.ordered) |
@@ -862,7 +845,6 @@ def _delegate_method(self, name, *args, **kwargs): |
862 | 845 | def _add_accessors(cls): |
863 | 846 | """ add in Categorical accessor methods """ |
864 | 847 |
|
865 | | - from pandas.core.arrays import Categorical |
866 | 848 | CategoricalIndex._add_delegate_accessors( |
867 | 849 | delegate=Categorical, accessors=["rename_categories", |
868 | 850 | "reorder_categories", |
|
0 commit comments