Incorrect assumption in Series.mask() · Issue #21891 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

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 "", line 1, in

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.

return self.where(~cond, other=other, inplace=inplace, axis=axis,
level=level, try_cast=try_cast,
errors=errors)

Expected Output

Series.mask(cond) succeeds.

Output of pd.show_versions()

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