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

BUG: Inplace DataFrame.mask not working on 2.0.0rc0 on mixed-type DataFrame. #51685

Closed
2 of 3 tasks
erovira opened this issue Feb 28, 2023 · 2 comments · Fixed by #51689
Closed
2 of 3 tasks

BUG: Inplace DataFrame.mask not working on 2.0.0rc0 on mixed-type DataFrame. #51685

erovira opened this issue Feb 28, 2023 · 2 comments · Fixed by #51689
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@erovira
Copy link

erovira commented Feb 28, 2023

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({'numbers': [1, 2], 'strings': ['hello', 'goodbye']})
mask_nulls = pd.DataFrame({'numbers': [True, False], 'strings': [False, True]})

df.mask(mask_nulls, inplace=True)

# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/home/eugenio/workspace/pandastest/200rc0/.venv/lib/python3.9/site-packages/pandas/core/frame.py", line 11554, in mask
#     return super().mask(
#   File "/home/eugenio/workspace/pandastest/200rc0/.venv/lib/python3.9/site-packages/pandas/core/generic.py", line 9961, in mask
#     return self.where(
#   File "/home/eugenio/workspace/pandastest/200rc0/.venv/lib/python3.9/site-packages/pandas/core/frame.py", line 11501, in where
#     return super().where(
#   File "/home/eugenio/workspace/pandastest/200rc0/.venv/lib/python3.9/site-packages/pandas/core/generic.py", line 9899, in where
#     return self._where(cond, other, inplace, axis, level)
#   File "/home/eugenio/workspace/pandastest/200rc0/.venv/lib/python3.9/site-packages/pandas/core/generic.py", line 9695, in _where
#     self._check_inplace_setting(other)
#   File "/home/eugenio/workspace/pandastest/200rc0/.venv/lib/python3.9/site-packages/pandas/core/generic.py", line 6087, in _check_inplace_setting
#     raise TypeError(
# TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value

Issue Description

The reasons I believe this is a bug in 2.0.0rc0 are:

  • If you remove the inplace=True from the pd.DataFrame.mask invocation, it works. Same behavior, except for the result being in a copy of course.
  • In Pandas 1.5.4 this does not happen, i.e. both inplace=True and inplace=False work properly.
  • Inplace mask works if you pass other=np.nan, which is strange.

Perhaps the behavior of the inplace masking in 2.0.0rc0 is correct, in which case I believe the inplace=False case should be consistent with it and equally fail.

Expected Behavior

The df DataFrame should look like the following, after the inplace masking. Just like it did in Pandas 1.5.4, or like it does in Pandas 2.0.0rc0 with inplace=False.

>>> df
   numbers strings
0      NaN   hello
1      2.0     NaN

Thanks for all the great work on Pandas! And apologies if this is duplicated or not an issue (and a misuse of the library on my part), this is my first opened issue on the repository.

Installed Versions

INSTALLED VERSIONS

commit : 1a2e300
python : 3.9.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-32-generic
Version : #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 30 17:03:34 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.0rc0
numpy : 1.24.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 58.1.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : None
qtpy : None
pyqt5 : None

@erovira erovira added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 28, 2023
@asishm
Copy link
Contributor

asishm commented Feb 28, 2023

Possibly due to #49156 that changed the default from np.nan to lib.no_default

specifying other=np.nan works without issues
i.e. df.mask(mask_nulls, np.nan inplace=True)

@phofl
Copy link
Member

phofl commented Feb 28, 2023

Yep can confirm

commit a75ea75a0d4efaf273ac70b6269adbfa400d0844
Author: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Date:   Tue Oct 18 02:22:40 2022 -0700

    API: Change Series/DataFrame.other default to no_default instead of np.nan (#49156)

cc @mroeschke

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 28, 2023
@phofl phofl added this to the 2.0 milestone Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants