Say I have a series s with some NaNs:
import numpy as np
import pandas as pd
s = pd.Series([1, 2, np.nan])
print(s)
0 1.0
1 2.0
2 NaN
dtype: float64
and I want to perform a comparison operation, but preserving NaNs. For example, say I want to check equality of each element to 2, producing:
0 False
1 True
2 NaN
dtype: object
But if I do s == 2, the NaN element evaluates to False:
print(s == 2)
0 False
1 True
2 False
dtype: bool
What's the best way to do this?