AttributeError:' list'对象没有属性' to_sql'

时间:2017-03-27 21:11:36

标签: python mysql

我试图编写一个python脚本来获取yahoo_finance数据并将其放在mysql数据库中。我可以通过漫长的方式做到这一点,但我现在发现了pandas和dataframe.to.sql。

我无法让它工作 - 我不断得到AttributeError。我做错了什么?

import yahoo_finance
from yahoo_finance import Share
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine


engine = create_engine('mysql://user:pass@localhost')
data = Share('YHOO')
df= data.get_historical ('2016-03-24', '2017-03-24')
df.to_sql(name = daily_price, con = engine, if_exists = 'append', flavor = 'mysql')

非常感谢!

1 个答案:

答案 0 :(得分:2)

您可以使用pandas_datareader获取Yahoo数据(过去曾为 X1 X2 X3 [1,] 2 2 2 ):

pandas.io

现在您可以致电>>> import pandas_datareader.data as web >>> data = web.get_data_yahoo('YHOO', '2016-03-24', '2017-03-24') >>> # Or: web.DataReader('YHOO', 'yahoo', '2016-03-24', '2017-03-24') >>> data Open High Low Close Volume Adj Close Date 2016-03-24 34.450001 34.869999 33.930000 34.860001 14118600 34.860001 2016-03-28 35.389999 35.450001 34.619999 35.230000 12981000 35.230000 2016-03-29 35.049999 36.430000 35.009998 36.320000 23603400 36.320000 2016-03-30 36.610001 37.279999 36.439999 36.560001 19849800 36.560001 ... >>> type(data) pandas.core.frame.DataFrame