Fill NaN values
<p>I have a dataframe</p>

<pre><code>TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR
2016-01-01 00:00:00 116 HC 250
2016-01-01 00:10:00 121 HC 250
2016-01-01 00:20:00 121 NaN 250
</code></pre>

<p>To use this dataframe, I must to fill the NaN values by (HC or HP) based on this condition:</p>

<pre><code>If (hour extracted from TIMESTAMP is in {0,1,2, 3, 4, 5, 22, 23}
</code></pre>

<p>So I replace NaN by HC,
else by HP.
I did this function:</p>

<pre><code>def prep_data(data):
    data['PERIODE_TARIF']=np.where(data['PERIODE_TARIF']in (0, 1,2, 3, 4, 5, 22, 23),'HC','HP')
    return data
</code></pre>

<p>But I get this error:</p>

<pre><code> ValueError                                Traceback (most recent call last)
 &lt;ipython-input-23-c1fb7e3d7b82&gt; in &lt;module&gt;()
 ----&gt; 1 prep_data(df_energy2)

 &lt;ipython-input-22-04bd325f91cd&gt; in prep_data(data)
       1 # Nettoyage des donnÃ©es
       2 def prep_data(data):
 ----&gt; 3     data['PERIODE_TARIF']=np.where(data['PERIODE_TARIF']in (0, 1),'HC','HP')
       4     return data

 C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\generic.py
 in __nonzero__(self)
     890         raise ValueError("The truth value of a {0} is ambiguous. "
     891                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
 --&gt; 892                          .format(self.__class__.__name__))
     893 
     894     __bool__ = __nonzero__

 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
</code></pre>

<p>How can I fix this?</p>

<p>use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html" rel="nofollow"><code>isin</code></a> to test for membership:</p>

<pre><code>data['PERIODE_TARIF']=np.where(data['PERIODE_TARIF'].isin([0, 1,2, 3, 4, 5, 22, 23]),'HC','HP')
</code></pre>

<p><code>in</code> doesn't understand how to evaluate an array of boolean values as it becomes ambiguous if you have more than 1 <code>True</code> in the array hence the error</p>

python pandas missing-data