Skip to content

BUG: df.mask with int df throws ValueError instead of TypeError #56330

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

Closed
3 tasks done
sfc-gh-rdurrani opened this issue Dec 4, 2023 · 9 comments
Closed
3 tasks done
Labels
Bug Error Reporting Incorrect or improved errors from pandas good first issue

Comments

@sfc-gh-rdurrani
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame(["a", "b", "c"], ["d", "e", "f"])
df.mask(df)

Issue Description

This should result in a ValueError that a boolean value must be passed, but instead, it throws a TypeError for ~ being applied to strings.

Expected Behavior

ValueError: Boolean array expected for the condition, not object

Installed Versions

INSTALLED VERSIONS

commit : 2a953cf
python : 3.10.13.final.0
python-bits : 64
OS : Darwin
OS-release : 23.1.0
Version : Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:45 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.3
numpy : 1.26.1
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.3
Cython : None
pytest : 7.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.17.2
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : 2023.10.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.4.50
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Also present on pandas 1.5.3

@sfc-gh-rdurrani sfc-gh-rdurrani added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 4, 2023
@rhshadrach
Copy link
Member

Thanks for the report. It seems to me the values of the string data is not the issue - there is no possible string value that won't raise. Thus, I don't think a ValueError would be appropriate here. From the Python docs on ValueError:

Raised when an operation or function receives an argument that has the right type but an inappropriate value

Rather, it's the type of the data being passed, and so TypeError is appropriate.

@rhshadrach rhshadrach added Error Reporting Incorrect or improved errors from pandas Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 5, 2023
@phofl
Copy link
Member

phofl commented Dec 5, 2023

Yep agreed, closing

@phofl phofl closed this as completed Dec 5, 2023
@sfc-gh-rdurrani
Copy link
Author

Does this mean that the code should be changed to throw a TypeError for dfs with int columns as well then?

@rhshadrach rhshadrach reopened this Dec 6, 2023
@rhshadrach
Copy link
Member

Confirmed on main - yes, I think these should be TypeErrors. PR to fix is welcome!

@rhshadrach rhshadrach changed the title BUG: df.mask with string df throws TypeError instead of ValueError BUG: df.mask with int df throws ValueError instead of TypeError Dec 6, 2023
@rhshadrach rhshadrach added good first issue and removed Closing Candidate May be closeable, needs more eyeballs labels Dec 6, 2023
@DarthKitten2130
Copy link
Contributor

Made a PR for this. Check it out.

@reshma045
Copy link

@rhshadrach which part of the code should be changed here? Could you please help?

@abhigyan631
Copy link

Is this issue still open? If yes, can I please work on it?

@TahirAlauddin
Copy link

TahirAlauddin commented Jan 23, 2024

@rhshadrach I wonder why is this issue still open. Didn't @DarthKitten2130 merged a PR already?

@rhshadrach
Copy link
Member

Thanks @TahirAlauddin - confirmed #56495 closed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants