Skip to content

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

Closed
@erovira

Description

@erovira

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions