Charlie Cai
Charlie Cai

Reputation: 79

ValueError: Shape of passed values is (6, 251), indices imply (6, 1)

I am getting an error and I'm not sure how to fix it. Here is my code:

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

How can I change my code to achieve my goal, change the dateform and delete the original one?

Upvotes: 0

Views: 604

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

In principle your code should work, you just need to indent it correctly, that is, you need to append the value of y to list1 inside the for loop.

for i in range(len(quotes)):
    x = date.fromordinal(int(quotes[i][0]))
    y = datetime.strftime(x, '%Y-%m-%d')
    list1.append(y)

Thereby list1 will have as many entries as quotes instead of only one (the last one). And the final dataframe will not complain about misshaped data.

Upvotes: 1

Related Questions