Skip to content

Commit b0f12ec

Browse files
author
tp
committed
changed according to comments
1 parent 913790d commit b0f12ec

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
@@ -183,9 +183,11 @@ def contains(cat, key, container):
183183
184184
Notes
185185
-----
186-
This method does not check for Nan values. Do that separately
186+
This method does not check for NaN values. Do that separately
187187
before calling this method.
188188
"""
189+
hash(key)
190+
189191
# get location of key in categories.
190192
# If a KeyError, the key isn't in categories, so logically
191193
# can't be in container either.
@@ -1897,9 +1899,8 @@ def __iter__(self):
18971899

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

19051906
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)