Cyrine Ezzahra Cyrine Ezzahra - 3 months ago 7
Python Question

Fill NaN values

I have a dataframe

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


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

If (hour extracted from TIMESTAMP is in {0,1,2, 3, 4, 5, 22, 23}


So I replace NaN by HC,
else by HP.
I did this function:

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


But I get this error:

ValueError Traceback (most recent call last)
<ipython-input-23-c1fb7e3d7b82> in <module>()
----> 1 prep_data(df_energy2)

<ipython-input-22-04bd325f91cd> in prep_data(data)
1 # Nettoyage des données
2 def prep_data(data):
----> 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()."
--> 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().


How can I fix this?

Answer

use isin to test for membership:

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

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