Skip to content

BUG: #40646

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
jaspersival opened this issue Mar 26, 2021 · 1 comment
Closed
3 tasks done

BUG: #40646

jaspersival opened this issue Mar 26, 2021 · 1 comment
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays

Comments

@jaspersival
Copy link

jaspersival commented Mar 26, 2021

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd


def test_mask_dataframe():
    df = pd.DataFrame(
        {
            "A": [1, 2, pd.NA],
            "B": [4, 5, 6]
        }, dtype=pd.Int64Dtype()
    )

    expected = pd.Series([4, 0, pd.NA], dtype=pd.Int64Dtype(), name="C")

    df["C"] = df["B"].mask(df["A"] == 2, 0)

    pd.testing.assert_series_equal(expected, df["C"])

E           AssertionError: ExtensionArray NA mask are different
E           
E           ExtensionArray NA mask values are different (33.33333 %)
E           [index]: [0, 1, 2]
E           [left]:  [False, False, True]
E           [right]: [False, False, False]

Actual output:

pd.Series([4, 0, 0], dtype=pd.Int64Dtype(), name="C")

Problem description

When performing the .mask method on a dataframe, it is expected that a value will be replaced by another value (in this case 0) when the condition result is True. However, when the condition result is pd.NA, it is now also being handled like it is True which is incorrect behaviour. I guess that the same problem happens when using the dataframe.where() method since .mask and .where are linked. It also happens for pd.Float64Dtype().

Expected Output

expected = pd.Series([4, 0, pd.NA], dtype=pd.Int64Dtype(), name="C")

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : 1.4.2
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@jaspersival jaspersival added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 26, 2021
@dsaxton dsaxton added NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 26, 2021
@dsaxton
Copy link
Member

dsaxton commented Mar 26, 2021

Thanks @jaspersival. I think this is a duplicate of #35429, let us know if not.

@dsaxton dsaxton closed this as completed Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

No branches or pull requests

2 participants