I am trying to write a Pandas dataframe (or can use a numpy array) to a mysql database using MysqlDB . MysqlDB doesn't seem understand 'nan' and my database throws out an error saying nan is not in the field list. I need to find a way to convert the 'nan' into a NoneType.
@bogatron has it right, you can use
where, it's worth noting that you can do this natively in pandas:
df1 = df.where((pd.notnull(df)), None)
Note: this changes the dtype of all columns to
In : df = pd.DataFrame([1, np.nan]) In : df Out: 0 0 1 1 NaN In : df1 = df.where((pd.notnull(df)), None) In : df1 Out: 0 0 1 1 None
df1 = df.astype(object).replace(np.nan, 'None')
As an aside, it's worth noting that for most use cases you don't need to replace NaN with None, see this question about the difference between NaN and None in pandas.
However, in this specific case it seems you do (at least at the time of this answer).