@@ -157,6 +157,55 @@ def _maybe_to_categorical(array):
157
157
return array
158
158
159
159
160
+ def contains (cat , key , container ):
161
+ """
162
+ Helper for membership check for ``key`` in ``cat``.
163
+
164
+ This is a helper method for :method:`__contains__`
165
+ and :class:`CategoricalIndex.__contains__`.
166
+
167
+ Returns True if ``key`` is in ``cat.categories`` and the
168
+ location of ``key`` in ``categories`` is in ``container``.
169
+
170
+ Parameters
171
+ ----------
172
+ cat : :class:`Categorical`or :class:`categoricalIndex`
173
+ key : a hashable object
174
+ The key to check membership for.
175
+ container : Container (e.g. list-like or mapping)
176
+ The container to check for membership in.
177
+
178
+ Returns
179
+ -------
180
+ is_in : bool
181
+ True if ``key`` is in ``self.categories`` and location of
182
+ ``key`` in ``categories`` is in ``container``, else False.
183
+
184
+ Notes
185
+ -----
186
+ This method does not check for Nan values. Do that separately
187
+ before calling this method.
188
+ """
189
+ # get location of key in categories.
190
+ # If a KeyError, the key isn't in categories, so logically
191
+ # can't be in container either.
192
+ try :
193
+ loc = cat .categories .get_loc (key )
194
+ except KeyError :
195
+ return False
196
+
197
+ # loc is the location of key in categories, but also the *value*
198
+ # for key in container. So, `key` may be in categories,
199
+ # but still not in `container`. Example ('b' in categories,
200
+ # but not in values):
201
+ # 'b' in Categorical(['a'], categories=['a', 'b']) # False
202
+ if is_scalar (loc ):
203
+ return loc in container
204
+ else :
205
+ # if categories is an IntervalIndex, loc is an array.
206
+ return any (loc_ in container for loc_ in loc )
207
+
208
+
160
209
_codes_doc = """The category codes of this categorical.
161
210
162
211
Level codes are an array if integer which are the positions of the real
@@ -1846,66 +1895,14 @@ def __iter__(self):
1846
1895
"""Returns an Iterator over the values of this Categorical."""
1847
1896
return iter (self .get_values ().tolist ())
1848
1897
1849
- @staticmethod
1850
- def _contains (key , categories , container ):
1851
- """
1852
- Helper for membership check for ``key``.
1853
-
1854
- This helper method is used in :method:`Categorical.__contains__`
1855
- and in :class:`CategoricalIndex.__contains__`.
1856
-
1857
- Returns True if ``key`` is in ``categories`` and the
1858
- location of ``key`` in ``categories`` is in ``container``.
1859
-
1860
- Parameters
1861
- ----------
1862
- key : a hashable object
1863
- The key to check membership for.
1864
- categories : Sequence
1865
- The possible values for ``key``. The location for ``key``
1866
- in ``categories`` is also its value in ``container``
1867
- container : Container (e.g. list-like or mapping)
1868
- The container to check for membership in.
1869
-
1870
- Returns
1871
- -------
1872
- is_in : bool
1873
- True if ``key`` is in ``categories`` and location of
1874
- ``key`` in ``categories`` is in ``container``, else False.
1875
-
1876
- Notes
1877
- -----
1878
- This method does not check for Nan values. Do that separately
1879
- before calling this method.
1880
- """
1881
-
1882
- # is key in categories? Then get its location in categories.
1883
- # If not (i.e. KeyError), its location logically can't be in
1884
- # container either.
1885
- try :
1886
- loc = categories .get_loc (key )
1887
- except KeyError :
1888
- return False
1889
-
1890
- # loc is the location of key in categories, but also the value
1891
- # for key in container. So, key may be in categories,
1892
- # but still not in container, this must be checked. Example:
1893
- # 'b' in Categorical(['a'], categories=['a', 'b']) # False
1894
- if is_scalar (loc ):
1895
- return loc in container
1896
- else :
1897
- # if categories is an IntervalIndex, loc is an array.
1898
- # Check if any scalar of the array is in the container
1899
- return any (loc_ in container for loc_ in loc )
1900
-
1901
1898
def __contains__ (self , key ):
1902
1899
"""Returns True if `key` is in this Categorical."""
1903
1900
hash (key )
1904
1901
1905
1902
if isna (key ): # if key is a NaN, check if any NaN is in self.
1906
1903
return self .isna ().any ()
1907
1904
1908
- return self . _contains ( key , self . categories , container = self ._codes )
1905
+ return contains ( self , key , container = self ._codes )
1909
1906
1910
1907
def _tidy_repr (self , max_vals = 10 , footer = True ):
1911
1908
""" a short repr displaying only max_vals and an optional (but default
0 commit comments