How do I make NaN numeric values to be lower than any other numeric value.during comparisons?

fly*_*our 3 python nan pandas

Bottomline, I want In a comparison, NaN numeric values to be lower than any other numeric value.

Lets say I have s1 and s2,

s1 = pd.Series([1, 3, np.nan, 5, np.nan, -1, np.nan])
s2 = pd.Series([2, 1, np.nan, np.nan, 2, np.nan, -1])
Run Code Online (Sandbox Code Playgroud)

When I compare them as s1 < s2 then I want the following behavior:

Out: 
0    True
1    False
2    False
3    False
4    True
5    False
6    True
Run Code Online (Sandbox Code Playgroud)

Rom*_*est 6

Simply with Series.fillna function and np.NINF constant:

In [256]: s2.fillna(np.NINF) > s1.fillna(np.NINF)                                                                                              
Out[256]: 
0     True
1    False
2    False
3    False
4     True
5    False
6     True
dtype: bool
Run Code Online (Sandbox Code Playgroud)
  • np.NINF - NumPy constant, floating point representation of negative infinity