Repository: pandas-dev/pandas

Issue:
Incorrect assumption in Series.mask() 
#### Code Sample, a copy-pastable example if possible

```python
pd.Series([1, 2]).where([True, False])
# 0    1.0
# 1    NaN
# dtype: float64
pd.Series([1, 2]).mask([True, False])
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/usr/local/lib/python3.7/site-packages/pandas/core/generic.py", line 7792, in mask
#     return self.where(~cond, other=other, inplace=inplace, axis=axis,
# TypeError: bad operand type for unary ~: 'list'
```
#### Problem description

With the same `cond`, `Series.where(cond)` succeeded but `Series.mask(cond)` failed.
It's because  `Series.mask()` assumes `cond` has `__invert__()` while it's not always ture.

https://github.com/pandas-dev/pandas/blob/365eac4d27353693a2b758c87fc4bceabb74817f/pandas/core/generic.py#L7944-L7946
#### Expected Output
`Series.mask(cond)` succeeds.

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Darwin
OS-release: 17.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: zh_CN.UTF-8
LOCALE: zh_CN.UTF-8

pandas: 0.23.3
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None


</details>



Discussion:
How weird!  Patch is certainly welcome!

Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -7941,6 +7941,10 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
         inplace = validate_bool_kwarg(inplace, 'inplace')
         cond = com._apply_if_callable(cond, self)
 
+        # see gh-21891
+        if not hasattr(cond, "__invert__"):
+            cond = np.array(cond)
+
         return self.where(~cond, other=other, inplace=inplace, axis=axis,
                           level=level, try_cast=try_cast,
                           errors=errors)


Test patch:
diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py
--- a/pandas/tests/frame/test_indexing.py
+++ b/pandas/tests/frame/test_indexing.py
@@ -2966,6 +2966,13 @@ def test_mask(self):
         assert_frame_equal(rs, df.mask(df <= 0, other))
         assert_frame_equal(rs, df.mask(~cond, other))
 
+        # see gh-21891
+        df = DataFrame([1, 2])
+        res = df.mask([[True], [False]])
+
+        exp = DataFrame([np.nan, 2])
+        tm.assert_frame_equal(res, exp)
+
     def test_mask_inplace(self):
         # GH8801
         df = DataFrame(np.random.randn(5, 3))
diff --git a/pandas/tests/series/indexing/test_boolean.py b/pandas/tests/series/indexing/test_boolean.py
--- a/pandas/tests/series/indexing/test_boolean.py
+++ b/pandas/tests/series/indexing/test_boolean.py
@@ -617,6 +617,13 @@ def test_mask():
     expected = Series([1, 2, np.nan, np.nan])
     assert_series_equal(result, expected)
 
+    # see gh-21891
+    s = Series([1, 2])
+    res = s.mask([True, False])
+
+    exp = Series([np.nan, 2])
+    tm.assert_series_equal(res, exp)
+
 
 def test_mask_inplace():
     s = Series(np.random.randn(5))