|
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.categorical import Categorical, contains |
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:
|
@@ -323,32 +322,14 @@ def _reverse_indexer(self):
|
323 | 322 |
|
324 | 323 | @Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
|
325 | 324 | def __contains__(self, key):
|
326 |
| - hash(key) |
327 |
| - |
328 |
| - if isna(key): # if key is a NaN, check if any NaN is in self. |
| 325 | + # if key is a NaN, check if any NaN is in self. |
| 326 | + if isna(key): |
329 | 327 | return self.hasnans
|
330 | 328 |
|
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) |
| 329 | + return contains(self, key, container=self._engine) |
348 | 330 |
|
349 | 331 | @Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
|
350 | 332 | def contains(self, key):
|
351 |
| - hash(key) |
352 | 333 | return key in self
|
353 | 334 |
|
354 | 335 | def __array__(self, dtype=None):
|
@@ -479,7 +460,6 @@ def where(self, cond, other=None):
|
479 | 460 | other = self._na_value
|
480 | 461 | values = np.where(cond, self.values, other)
|
481 | 462 |
|
482 |
| - from pandas.core.arrays import Categorical |
483 | 463 | cat = Categorical(values,
|
484 | 464 | categories=self.categories,
|
485 | 465 | ordered=self.ordered)
|
@@ -862,7 +842,6 @@ def _delegate_method(self, name, *args, **kwargs):
|
862 | 842 | def _add_accessors(cls):
|
863 | 843 | """ add in Categorical accessor methods """
|
864 | 844 |
|
865 |
| - from pandas.core.arrays import Categorical |
866 | 845 | CategoricalIndex._add_delegate_accessors(
|
867 | 846 | delegate=Categorical, accessors=["rename_categories",
|
868 | 847 | "reorder_categories",
|
|
0 commit comments