我收到错误,我不确定如何修复它。 这是我的代码:
from matplotlib.finance import quotes_historical_yahoo_ochl
from datetime import date
from datetime import datetime
import pandas as pd
today = date.today()
start = (today.year-1, today.month, today.day)
quotes = quotes_historical_yahoo_ochl('AXP', start, today)
fields = ['date', 'open', 'close', 'high', 'low', 'volume']
list1 = []
for i in range(len(quotes)):
x = date.fromordinal(int(quotes[i][0]))
y = datetime.strftime(x, '%Y-%m-%d')
list1.append(y)
quotesdf = pd.DataFrame(quotes, index = list1, columns = fields)
quotesdf = quotesdf.drop(['date'], axis = 1)
print quotesdf
如何更改代码以实现目标,更改日期格式并删除原始日期?
答案 0 :(得分:1)
原则上你的代码应该有效,你只需要正确地缩进它,也就是说,你需要将y
的值附加到list1
里面 for循环
for i in range(len(quotes)):
x = date.fromordinal(int(quotes[i][0]))
y = datetime.strftime(x, '%Y-%m-%d')
list1.append(y)
因此list1
将包含与quotes
一样多的条目,而不是仅包含一个(最后一个)。最终的数据框架不会抱怨错误的数据。