id,1,2,1ah,2ah,ah_line
13399,1.08,10.03,2.0,1.89,-13.5
13400,1.07,11.25,2.0,1.89,-19.5
13401,1.22,4.84,1.97,1.93,-9.0
13402,3.09,1.43,1.99,1.92,5.0
我正在尝试通过pandas DataFrame.to_sql
方法填充mysql数据库,并且出于某种宽松的原因它正在提升KeyError: <class 'numpy.float64'>
。
print(df.dtypes)
id int64
1 float64
2 float64
1ah float64
2ah float64
ah_line float64
dtype: object
我画了一个空白,有什么想法吗?我做了很多次,从来没有犯过这个错误。
修改
奇怪的是,它与基本相同的数据一起运行良好,例如:
id,q1,q2,q3,q4,ot1,ot2
13399,16.0,17.0,17.0,19.0,na,na
13400,20.0,17.0,20.0,16.0,na,na
13401,18.0,19.0,17.0,18.0,na,na
13402,15.0,17.0,17.0,16.0,na,na
信息:
pandas==0.13.1
cymysql==0.7.2
完整的Tracebback:
Traceback (most recent call last):
File "/home/bartek/Desktop/buckypy3/plk-working.py", line 389, in <module>
df.to_sql('plk_odds', con=conn, flavor='mysql', if_exists='append')
File "/home/bartek/py3/lib/python3.3/site-packages/pandas/core/frame.py", line 1261, in to_sql
self, name, con, flavor=flavor, if_exists=if_exists, **kwargs)
File "/home/bartek/py3/lib/python3.3/site-packages/pandas/io/sql.py", line 238, in write_frame
func(frame, name, safe_names, cur)
File "/home/bartek/py3/lib/python3.3/site-packages/pandas/io/sql.py", line 264, in _write_mysql
cur.executemany(insert_query, data)
File "/home/bartek/py3/lib/python3.3/site-packages/cymysql/cursors.py", line 144, in executemany
self.execute(query, params)
File "/home/bartek/py3/lib/python3.3/site-packages/cymysql/cursors.py", line 118, in execute
escaped_args = tuple(conn.escape(arg) for arg in args)
File "/home/bartek/py3/lib/python3.3/site-packages/cymysql/cursors.py", line 118, in <genexpr>
escaped_args = tuple(conn.escape(arg) for arg in args)
File "/home/bartek/py3/lib/python3.3/site-packages/cymysql/connections.py", line 294, in escape
return escape_item(obj, self.charset, self.encoders)
File "/home/bartek/py3/lib/python3.3/site-packages/cymysql/converters.py", line 354, in escape_item
encoder = encoders[type(val)]
KeyError: <class 'numpy.float64'>
答案 0 :(得分:1)
这似乎是pandas 0.13.1中的一个错误(我可以使用cymysql
作为pymysql
重现它),但我不确定该错误的原因。
我用pandas 0.14测试了它,问题似乎已经解决了(至少用pymysql
)。在0.14中有一个sql模块的大型重构(参见here),现在使用sqlalchemy来支持不同的数据库风格。最终的0.14将于2014年5月31日发布。