|
39 | 39 | from pandas.util._decorators import (
|
40 | 40 | Appender, cache_readonly, deprecate_kwarg, Substitution)
|
41 | 41 |
|
| 42 | +import pandas.core.algorithms as algorithms |
| 43 | + |
42 | 44 | from pandas.io.formats.terminal import get_terminal_size
|
43 | 45 | from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs
|
44 | 46 | from pandas.core.config import get_option
|
@@ -2216,6 +2218,60 @@ def _concat_same_type(self, to_concat):
|
2216 | 2218 | def _formatting_values(self):
|
2217 | 2219 | return self
|
2218 | 2220 |
|
| 2221 | + def isin(self, values): |
| 2222 | + """ |
| 2223 | + Check whether `values` are contained in Categorical. |
| 2224 | +
|
| 2225 | + Return a boolean NumPy Array showing whether each element in |
| 2226 | + the Categorical matches an element in the passed sequence of |
| 2227 | + `values` exactly. |
| 2228 | +
|
| 2229 | + Parameters |
| 2230 | + ---------- |
| 2231 | + values : set or list-like |
| 2232 | + The sequence of values to test. Passing in a single string will |
| 2233 | + raise a ``TypeError``. Instead, turn a single string into a |
| 2234 | + list of one element. |
| 2235 | +
|
| 2236 | + Returns |
| 2237 | + ------- |
| 2238 | + isin : numpy.ndarray (bool dtype) |
| 2239 | +
|
| 2240 | + Raises |
| 2241 | + ------ |
| 2242 | + TypeError |
| 2243 | + * If `values` is not a set or list-like |
| 2244 | +
|
| 2245 | + See Also |
| 2246 | + -------- |
| 2247 | + pandas.Series.isin : equivalent method on Series |
| 2248 | +
|
| 2249 | + Examples |
| 2250 | + -------- |
| 2251 | +
|
| 2252 | + >>> s = pd.Categorical(['lama', 'cow', 'lama', 'beetle', 'lama', |
| 2253 | + ... 'hippo']) |
| 2254 | + >>> s.isin(['cow', 'lama']) |
| 2255 | + array([ True, True, True, False, True, False]) |
| 2256 | +
|
| 2257 | + Passing a single string as ``s.isin('lama')`` will raise an error. Use |
| 2258 | + a list of one element instead: |
| 2259 | +
|
| 2260 | + >>> s.isin(['lama']) |
| 2261 | + array([ True, False, True, False, True, False]) |
| 2262 | + """ |
| 2263 | + from pandas.core.series import _sanitize_array |
| 2264 | + if not is_list_like(values): |
| 2265 | + raise TypeError("only list-like objects are allowed to be passed" |
| 2266 | + " to isin(), you passed a [{values_type}]" |
| 2267 | + .format(values_type=type(values).__name__)) |
| 2268 | + values = _sanitize_array(values, None, None) |
| 2269 | + null_mask = np.asarray(isna(values)) |
| 2270 | + code_values = self.categories.get_indexer(values) |
| 2271 | + code_values = code_values[null_mask | (code_values >= 0)] |
| 2272 | + return algorithms.isin(self.codes, code_values) |
| 2273 | + |
| 2274 | + |
2219 | 2275 | # The Series.cat accessor
|
2220 | 2276 |
|
2221 | 2277 |
|
|
0 commit comments