Search code examples
pandasnumpycomparisonnanmissing-data

Don't compare missing / NaN values


How to compare two Series and leave NaN values? For example:

s1 = pd.Series([np.nan, 1, 3])

s2 = pd.Series([0, 2, 3])

s1.eq(s2).astype(int)

Output:

0    0
1    0
2    1
dtype: int64

Desired result:

0    NaN
1    0.0
2    1.0
dtype: float64

Solution

  • Try this if you allow float in the end

    s1.eq(s2).mask(s1.isna() | s2.isna())
    

    or this if you want to keep boolean

    s1.eq(s2).mask(s1.isna() | s2.isna()).astype("boolean")