Repository: pandas-dev/pandas

Issue:
pd.Categorical.contains([item, otheritem]) raises ValueError
#### Code Sample, a copy-pastable example if possible

```python
In [2]: idx = pd.CategoricalIndex([1, 2, 3])

In [3]: list('a') in idx
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-76f69ba8dbb9> in <module>()
----> 1 list('a') in idx

/home/nobackup/repo/pandas/pandas/core/indexes/category.py in __contains__(self, key)
    327             return self.hasnans
    328 
--> 329         return contains(self, key, container=self._engine)
    330 
    331     @Appender(_index_shared_docs['contains'] % _index_doc_kwargs)

/home/nobackup/repo/pandas/pandas/core/arrays/categorical.py in contains(cat, key, container)
    187     before calling this method.
    188     """
--> 189     hash(key)
    190 
    191     # get location of key in categories.

TypeError: unhashable type: 'list'

In [4]: list('ab') in idx
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-0b6b309a5156> in <module>()
----> 1 list('ab') in idx

/home/nobackup/repo/pandas/pandas/core/indexes/category.py in __contains__(self, key)
    324     def __contains__(self, key):
    325         # if key is a NaN, check if any NaN is in self.
--> 326         if isna(key):
    327             return self.hasnans
    328 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```

#### Problem description

This is the only case, to the best of my (very limited - I didn't investigate much) knowledge, in which the ``contains`` method of an index raises anything different from ``TypeError: unhashable type:...``.

xref #21313

#### Expected Output

``TypeError: unhashable type: 'list'``

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: 2b136054db3b8f4c8d1aa33160c4bbe3b8ce940e
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-6-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8

pandas: 0.24.0.dev0+216.g2b136054d
pytest: 3.5.0
pip: 9.0.1
setuptools: 39.2.0
Cython: 0.25.2
numpy: 1.14.3
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.5.6
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.0dev
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.2.2.post1153+gff6786446
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.3.0
xlsxwriter: 0.9.6
lxml: 4.1.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.2.1
gcsfs: None


</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py
--- a/pandas/core/arrays/categorical.py
+++ b/pandas/core/arrays/categorical.py
@@ -2020,7 +2020,7 @@ def __contains__(self, key):
         Returns True if `key` is in this Categorical.
         """
         # if key is a NaN, check if any NaN is in self.
-        if isna(key):
+        if is_scalar(key) and isna(key):
             return self.isna().any()
 
         return contains(self, key, container=self._codes)
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -60,6 +60,7 @@
     is_extension_array_dtype,
     is_extension_type,
     is_float_dtype,
+    is_hashable,
     is_integer,
     is_integer_dtype,
     is_iterator,
@@ -2954,16 +2955,12 @@ def __getitem__(self, key):
         key = lib.item_from_zerodim(key)
         key = com.apply_if_callable(key, self)
 
-        # shortcut if the key is in columns
-        try:
+        if is_hashable(key):
+            # shortcut if the key is in columns
             if self.columns.is_unique and key in self.columns:
                 if self.columns.nlevels > 1:
                     return self._getitem_multilevel(key)
                 return self._get_item_cache(key)
-        except (TypeError, ValueError):
-            # The TypeError correctly catches non hashable "key" (e.g. list)
-            # The ValueError can be removed once GH #21729 is fixed
-            pass
 
         # Do we have a slicer (on rows)?
         indexer = convert_to_index_sliceable(self, key)
diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py
--- a/pandas/core/indexes/category.py
+++ b/pandas/core/indexes/category.py
@@ -407,7 +407,7 @@ def _reverse_indexer(self):
     @Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
     def __contains__(self, key):
         # if key is a NaN, check if any NaN is in self.
-        if isna(key):
+        if is_scalar(key) and isna(key):
             return self.hasnans
 
         return contains(self, key, container=self._engine)


Test patch:
diff --git a/pandas/tests/arrays/categorical/test_operators.py b/pandas/tests/arrays/categorical/test_operators.py
--- a/pandas/tests/arrays/categorical/test_operators.py
+++ b/pandas/tests/arrays/categorical/test_operators.py
@@ -417,3 +417,15 @@ def test_contains_interval(self, item, expected):
         cat = Categorical(pd.IntervalIndex.from_breaks(range(3)))
         result = item in cat
         assert result is expected
+
+    def test_contains_list(self):
+        # GH#21729
+        cat = Categorical([1, 2, 3])
+
+        assert "a" not in cat
+
+        with pytest.raises(TypeError, match="unhashable type"):
+            ["a"] in cat
+
+        with pytest.raises(TypeError, match="unhashable type"):
+            ["a", "b"] in cat
diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py
--- a/pandas/tests/indexes/test_category.py
+++ b/pandas/tests/indexes/test_category.py
@@ -276,6 +276,18 @@ def test_contains_interval(self, item, expected):
         result = item in ci
         assert result is expected
 
+    def test_contains_list(self):
+        # GH#21729
+        idx = pd.CategoricalIndex([1, 2, 3])
+
+        assert "a" not in idx
+
+        with pytest.raises(TypeError, match="unhashable type"):
+            ["a"] in idx
+
+        with pytest.raises(TypeError, match="unhashable type"):
+            ["a", "b"] in idx
+
     def test_map(self):
         ci = pd.CategoricalIndex(list("ABABC"), categories=list("CBA"), ordered=True)
         result = ci.map(lambda x: x.lower())