Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect assumption in Series.mask() #21891

Closed
holymonson opened this issue Jul 13, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@holymonson
Copy link
Contributor

commented Jul 13, 2018

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 "<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.

pandas/pandas/core/generic.py

Lines 7944 to 7946 in 365eac4

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

@gfyoung gfyoung added Bug Algos labels Jul 13, 2018

@gfyoung

This comment has been minimized.

Copy link
Member

commented Jul 13, 2018

How weird! Patch is certainly welcome!

gfyoung added a commit to forking-repos/pandas that referenced this issue Jul 16, 2018

@jreback jreback added this to the 0.24.0 milestone Jul 16, 2018

gfyoung added a commit to forking-repos/pandas that referenced this issue Jul 17, 2018

jreback added a commit that referenced this issue Jul 17, 2018

aeltanawy pushed a commit to aeltanawy/pandas that referenced this issue Jul 20, 2018

alimcmaster1 added a commit to alimcmaster1/pandas that referenced this issue Aug 12, 2018

Sup3rGeo added a commit to Sup3rGeo/pandas that referenced this issue Oct 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.