Skip to content

Commit

Permalink
changed according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tp committed Jun 19, 2018
1 parent a850131 commit 89e92da
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
9 changes: 5 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ def contains(cat, key, container):
Notes
-----
This method does not check for Nan values. Do that separately
This method does not check for NaN values. Do that separately
before calling this method.
"""
hash(key)

# get location of key in categories.
# If a KeyError, the key isn't in categories, so logically
# can't be in container either.
Expand Down Expand Up @@ -1898,9 +1900,8 @@ def __iter__(self):

def __contains__(self, key):
"""Returns True if `key` is in this Categorical."""
hash(key)

if isna(key): # if key is a NaN, check if any NaN is in self.
# if key is a NaN, check if any NaN is in self.
if isna(key):
return self.isna().any()

return contains(self, key, container=self._codes)
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,14 @@ def _reverse_indexer(self):

@Appender(_index_shared_docs['__contains__'] % _index_doc_kwargs)
def __contains__(self, key):
hash(key)

if isna(key): # if key is a NaN, check if any NaN is in self.
# if key is a NaN, check if any NaN is in self.
if isna(key):
return self.hasnans

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

@Appender(_index_shared_docs['contains'] % _index_doc_kwargs)
def contains(self, key):
hash(key)
return key in self

def __array__(self, dtype=None):
Expand Down
16 changes: 0 additions & 16 deletions pandas/tests/categorical/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,6 @@ def test_isin_empty(empty):
tm.assert_numpy_array_equal(expected, result)


def test_contains():
# GH21508
c = pd.Categorical(list('aabbca'), categories=list('cab'))

assert 'b' in c
assert 'z' not in c
assert np.nan not in c

# assert codes NOT in index
assert 0 not in c
assert 1 not in c

c = pd.Categorical(list('aabbca') + [np.nan], categories=list('cab'))
assert np.nan in c


class TestTake(object):
# https://github.com/pandas-dev/pandas/issues/20664

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,20 @@ def test_numeric_like_ops(self):

# invalid ufunc
pytest.raises(TypeError, lambda: np.log(s))

def test_contains(self):
# GH21508
c = pd.Categorical(list('aabbca'), categories=list('cab'))

assert 'b' in c
assert 'z' not in c
assert np.nan not in c
with pytest.raises(TypeError):
assert [1] in c

# assert codes NOT in index
assert 0 not in c
assert 1 not in c

c = pd.Categorical(list('aabbca') + [np.nan], categories=list('cab'))
assert np.nan in c

0 comments on commit 89e92da

Please sign in to comment.