Skip to content

BUG: Float64Dtype and isnull() functions unexpected behavior #53887

Open
@jayantsahewal

Description

@jayantsahewal

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
import numpy as np

def weird_behavior(df, cols):
    print(df.dtypes.tolist())
    for col in cols:
        print(df[col].isna().sum(), df[col].apply(pd.isna).sum())
        print(df[col].isnull().sum(), df[col].apply(pd.isnull).sum())
    print()

test_1_df = pd.DataFrame(data=[[1.0, 0.0], [1.0, 1], [1.0, 1], [4.0, 0], [0.0, 0]], columns=['a', 'b'], dtype=pd.Float64Dtype())
test_1_df['c'] = test_1_df['a'] / test_1_df['b']
test_1_df['d'] = float('inf')
test_1_df['e'] = np.nan
test_2_df = test_1_df.astype(pd.Float64Dtype())
test_3_df = test_1_df.astype(np.float64)
cols = ['c', 'd', 'e']
weird_behavior(test_1_df, cols)
weird_behavior(test_2_df, cols)
weird_behavior(test_3_df, cols)

test_4_df = pd.DataFrame(data=[[1.0, 0.0], [1.0, 1], [1.0, 1], [4.0, 0], [0.0, 0]], columns=['a', 'b'])
test_4_df['c'] = test_4_df['a'] / test_4_df['b']
test_4_df['d'] = float('inf')
test_4_df['e'] = np.nan
test_5_df = test_4_df.astype(pd.Float64Dtype())
test_6_df = test_4_df.astype(np.float64)
cols = ['c', 'd', 'e']
weird_behavior(test_4_df, cols)
weird_behavior(test_5_df, cols)
weird_behavior(test_6_df, cols)

[Float64Dtype(), Float64Dtype(), Float64Dtype(), dtype('float64'), dtype('float64')]
0 1
0 1
0 0
0 0
5 5
5 5

[Float64Dtype(), Float64Dtype(), Float64Dtype(), Float64Dtype(), Float64Dtype()]
0 1
0 1
0 0
0 0
5 5
5 5

[dtype('float64'), dtype('float64'), dtype('float64'), dtype('float64'), dtype('float64')]
1 1
1 1
0 0
0 0
5 5
5 5

[dtype('float64'), dtype('float64'), dtype('float64'), dtype('float64'), dtype('float64')]
1 1
1 1
0 0
0 0
5 5
5 5

[Float64Dtype(), Float64Dtype(), Float64Dtype(), Float64Dtype(), Float64Dtype()]
1 1
1 1
0 0
0 0
5 5
5 5

[dtype('float64'), dtype('float64'), dtype('float64'), dtype('float64'), dtype('float64')]
1 1
1 1
0 0
0 0
5 5
5 5


### Issue Description

Specifying `dtype=pd.Float64Dtype()` while creating a pandas dataFrame leads to inconsistent results in using `series.isnull()` vs. `series.apply(pd.isnull)`. 

If I don't specify the dtype while creating a pandas dataFrame, it works as expected in using `series.isnull()` vs. `series.apply(pd.isnull)`

### Expected Behavior

1 1
1 1
0 0
0 0
5 5
5 5


### Installed Versions

<details>

commit           : 965ceca9fd796940050d6fc817707bba1c4f9bff
python           : 3.10.8.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.10.157-139.675.amzn2.x86_64
Version          : #1 SMP Thu Dec 8 01:29:11 UTC 2022
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 2.0.2
numpy            : 1.23.4
pytz             : 2022.7
dateutil         : 2.8.2
setuptools       : 65.6.3
pip              : 23.1
Cython           : 0.29.33
pytest           : 7.2.0
hypothesis       : None
sphinx           : 6.1.2
blosc            : None
feather          : None
xlsxwriter       : 3.0.6
lxml.etree       : 4.9.2
html5lib         : None
pymysql          : 1.0.3
psycopg2         : 2.9.3
jinja2           : 3.1.2
IPython          : 7.34.0
pandas_datareader: None
bs4              : 4.11.1
bottleneck       : 1.3.5
brotli           : 
fastparquet      : None
fsspec           : 2022.11.0
gcsfs            : None
matplotlib       : 3.6.2
numba            : 0.56.4
numexpr          : 2.7.3
odfpy            : None
openpyxl         : 3.0.10
pandas_gbq       : None
pyarrow          : 8.0.0
pyreadstat       : None
pyxlsb           : None
s3fs             : 0.4.2
scipy            : 1.10.0
snappy           : None
sqlalchemy       : 1.4.46
tables           : 3.7.0
tabulate         : None
xarray           : None
xlrd             : None
zstandard        : None
tzdata           : 2023.3
qtpy             : 2.3.0
pyqt5            : None

</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNA - MaskedArraysRelated to pd.NA and nullable extension arraysPDEP missing valuesIssues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions