python pandas Ignore Nan in integer comparisons
<p>I am trying to create dummy variables based on integer comparisons in series where Nan is common.  A > comparison raises errors if there are any Nan values, but I want the comparison to return a Nan.  I understand that I could use fillna() to replace Nan with a value that I know will be false, but I would hope there is a more elegant way to do this.  I would need to change the value in fillna() if I used less than, or used a variable that could be positive or negative, and that is one more opportunity to create errors.  Is there any way to make 30 &lt; Nan = Nan?</p>

<p>To be clear, I want this:</p>

<pre><code>df['var_dummy'] = df[df['var'] &gt;= 30].astype('int')
</code></pre>

<p>to return a null if var is null, 1 if it is 30+, and 0 otherwise.  Currently I get ValueError: cannot reindex from a duplicate axis.</p>

<p>Here's a way:</p>

<pre><code>s1 = pd.Series([1, 3, 4, 2, np.nan, 5, np.nan, 7])
s2 = pd.Series([2, 1, 5, 5, np.nan, np.nan, 2, np.nan])

(s1 &lt; s2).mask(s1.isnull() | s2.isnull(), np.nan)
Out: 
0    1.0
1    0.0
2    1.0
3    1.0
4    NaN
5    NaN
6    NaN
7    NaN
dtype: float64
</code></pre>

<p>This masks the boolean array returned from <code>(s1 &lt; s2)</code> if any of them is NaN. In that case, it returns NaN. But you cannot have NaNs in a boolean array so it will be casted as float.</p>

python pandas