abu*_*eau 2 python sqlite numpy pandas quandl
代码很简单:
import Quandl
import sqlite3
myData = Quandl.get("DMDRN/AAPL_ALLFINANCIALRATIOS")
cnx = sqlite3.connect("APPL.db")
myData.to_sql('AAPL', cnx)
Run Code Online (Sandbox Code Playgroud)
我打电话给Quandl API.它给了我一个熊猫数据帧.当我尝试将数据提交到SQL表时,我收到此错误
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
Run Code Online (Sandbox Code Playgroud)
索引是时间戳.
我试过这个1- 如何将Pandas数据帧写入带有索引的sqlite 2-将索引设置为另一个值+ 将numpy.datetime64转换为python中的字符串对象
对于第一个我仍然得到错误绑定参数1和2不起作用.
如果我想将数据帧提交到sqlite表并将日期保留为索引,那么我应该做什么(或者最好的方法).
问题是sqlite连接尚不支持编写datetime64值.随着即将推出的pandas 0.15,这个bug将被修复.已经支持编写索引(来自pandas 0.14),并且可以使用index关键字(默认值为True)进行控制.
你有一些选择来解决这个问题:
使用sqlalchemy建立连接(此时至少需要0.14),因为这已经支持写日期时间值:
import sqlalchemy
engine = sqlalchemy.create_engine('sqlite:///APPL.db')
myData.to_sql('AAPL', engine, index=True)
Run Code Online (Sandbox Code Playgroud)将datetime索引转换为字符串(然后您可以继续直接使用sqlite连接).你可以这样做:
myData.index = myData.index.map(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))
Run Code Online (Sandbox Code Playgroud)使用pandas开发版(https://github.com/pydata/pandas)