ValueError:传递值的形状为(6,251),索引暗示(6,1)

时间:2016-11-08 08:31:11

标签: python-2.7 pandas matplotlib

我收到错误,我不确定如何修复它。 这是我的代码:

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

enter image description here

如何更改代码以实现目标,更改日期格式并删除原始日期?

1 个答案:

答案 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一样多的条目,而不是仅包含一个(最后一个)。最终的数据框架不会抱怨错误的数据。