Skip to content

Commit 89e92da

Browse files
author
tp
committed
changed according to comments
1 parent a850131 commit 89e92da

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

pandas/core/arrays/categorical.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ def contains(cat, key, container):
184184
185185
Notes
186186
-----
187-
This method does not check for Nan values. Do that separately
187+
This method does not check for NaN values. Do that separately
188188
before calling this method.
189189
"""
190+
hash(key)
191+
190192
# get location of key in categories.
191193
# If a KeyError, the key isn't in categories, so logically
192194
# can't be in container either.
@@ -1898,9 +1900,8 @@ def __iter__(self):
18981900

18991901
def __contains__(self, key):
19001902
"""Returns True if `key` is in this Categorical."""
1901-
hash(key)
1902-
1903-
if isna(key): # if key is a NaN, check if any NaN is in self.
1903+
# if key is a NaN, check if any NaN is in self.
1904+
if isna(key):
19041905
return self.isna().any()
19051906

19061907
return contains(self, key, container=self._codes)

pandas/core/indexes/category.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,14 @@ def _reverse_indexer(self):
322322

323323
@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
324324
def __contains__(self, key):
325-
hash(key)
326-
327-
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):
328327
return self.hasnans
329328

330329
return contains(self, key, container=self._engine)
331330

332331
@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
333332
def contains(self, key):
334-
hash(key)
335333
return key in self
336334

337335
def __array__(self, dtype=None):

pandas/tests/categorical/test_algos.py

-16
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,6 @@ def test_isin_empty(empty):
7171
tm.assert_numpy_array_equal(expected, result)
7272

7373

74-
def test_contains():
75-
# GH21508
76-
c = pd.Categorical(list('aabbca'), categories=list('cab'))
77-
78-
assert 'b' in c
79-
assert 'z' not in c
80-
assert np.nan not in c
81-
82-
# assert codes NOT in index
83-
assert 0 not in c
84-
assert 1 not in c
85-
86-
c = pd.Categorical(list('aabbca') + [np.nan], categories=list('cab'))
87-
assert np.nan in c
88-
89-
9074
class TestTake(object):
9175
# https://github.com/pandas-dev/pandas/issues/20664
9276

pandas/tests/categorical/test_operators.py

+17
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,20 @@ def test_numeric_like_ops(self):
291291

292292
# invalid ufunc
293293
pytest.raises(TypeError, lambda: np.log(s))
294+
295+
def test_contains(self):
296+
# GH21508
297+
c = pd.Categorical(list('aabbca'), categories=list('cab'))
298+
299+
assert 'b' in c
300+
assert 'z' not in c
301+
assert np.nan not in c
302+
with pytest.raises(TypeError):
303+
assert [1] in c
304+
305+
# assert codes NOT in index
306+
assert 0 not in c
307+
assert 1 not in c
308+
309+
c = pd.Categorical(list('aabbca') + [np.nan], categories=list('cab'))
310+
assert np.nan in c

0 commit comments

Comments
 (0)