[Python] Why does pd.isnull behave differently on DataFrame vs. single element?

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm noticing an inconsistency in how pd.isnull behaves.

    Given that pd.isnull('nan') returns False, but pd.isnull(float('nan')) returns True, I would have expected that applying pd.isnull to a DataFrame column previously converted to the 'Float64' dtype would return True for any value that was originally the string 'nan'. But that's not what I'm seeing.

    Here's an example:

    import pandas as pd
    import numpy as np

    df = pd.DataFrame([['1', pd.NA, 'nan'], ['re', 'ui1', np.nan]], columns=['a', 'b', 'c'])

    pd.isnull(df['c']) # Returns [False, True] ==> expected
    pd.isnull(df.loc[0, 'c']) # Returns False ==> expected
    pd.isnull(df[['c']].astype('Float64')) # Returns [False, True] ==> unexpected?
    pd.isnull(df[['c']].astype('Float64').values) # Returns [True, True] ==> expected?
    pd.isnull(df[['c']].astype('Float64').loc[0, 'c']) # Returns True ==> expected


    Why does pd.isnull exhibit different behavior when applied to an entire column or dataset compared to a single element? Am I missing something?

    python: 3.9.12 ; pandas: 2.2.3 ; numpy:1.26.4

    Continue reading...

Compartilhe esta Página