我在执行以下函数时收到此错误,这是因为尝试将NaN插入数字列。始终用0替换NaN确实可以,但是用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数字中使用
最佳答案
好吧,Pandas有自己独特的DataFrame.to_sql()方法,该方法会自动处理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)关于python - 在Python Pandas中将NaN转换为Oracle Null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48299282/