Wednesday 15 September 2021

Python Panda's - Cannot convert non-finite values (NA or inf) to integer

 In the .csv that we're processing we have a column thats 'total_in_thousands' .  

In that column the values are all in decimals and I presume the legacy code would like them to be integers. 

csv = csv.astype({'total_uv_in_thousands' : 'int64'})

However this giving me this error. 


1 astype_nansafe 2 raise ValueError("Cannot convert non-finite values (NA or inf) to integer") 3ValueError: Cannot convert non-finite values (NA or inf) to integer


To resolve this I’ve changed on NaN’s in this column to zero and it now works.
csv = csv.fillna({'total_uv_in_thousands' : 0}).astype({'total_uv_in_thousands' : 'int64'})


This works. 


No comments: