执行以下功能时出现此错误&这是因为尝试将NaN插入到数字列中。将NaN替换为0可以正常工作,但是用None替换则不行。任何想法将不胜感激
def insertData(table_name, schema_name, df):
if not df.empty:
#input_data[file].fillna(0, inplace=True) # This works but replacing with 0's is not ideal
df.where(pd.notnull(df), None) #This does not work
values = df.to_dict(orient='records')
table = sa.Table(table_name , conndict['meta'], autoload=True, schema=schema_name)
result_proxy = conndict['conn'].execute(table.insert(), values)
return result_proxy
else:
return None
(cx_Oracle.DatabaseError)DPI-1055:值不是数字(NaN),不能在Oracle数字中使用
答案 0 :(得分:2)
NaN
- > NULL
自动转换:
df.to_sql('tab_name', sql_alchemy_conn, if_exists='append', index=False)
PS,以防你的DF有字符串(object
)列 - 使用dtype
参数(see an example in this answer)