0

I'm trying to fetch prices of three stock tickers from morningstar and put them into a data frame, but pd.DataFrame keeps giving me NaN values.

all_data = {}

for ticker in ['AAL', 'ALK','WTI']:
     all_data[ticker] = data.DataReader(ticker, 'morningstar', '2014-06-01','2016-06-13')

enter image description here

price = pd.DataFrame({ticker: data['Close'] for ticker, data in all_data.items()})
print(price.head(5))

enter image description here

Ideally I want the pd.DataFrame to return a data frame of four columns (date, closing price for ticker 1, closing price for ticker 2 and closing price for ticker 3) but it kept returning NaN values for the second and third tickers.

I'm wondering how can I fix the code to get intended results?

Thank you very much!

1
  • You can make the code samples more readable by enclosing them in code format tags. See the tools above the edit bar or check out the help here. Commented Apr 6, 2018 at 2:45

1 Answer 1

1

I think the dict value is dataframe, base on your image , so when you doing data['Close'] it will keep its index, different index concat will return NaN for miss match

all_data = {}

for ticker in ['AAL', 'ALK','WTI']:
     all_data[ticker] = data.DataReader(ticker, 'morningstar', '2014-06-01','2016-06-13')

price = pd.DataFrame({ticker: data['Close'].values for ticker, data in all_data.items()})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I think the indexing might be the reason too, but how can I fix the issue so there won't be NaN values?
@CatherineZhang Adding .values(look at my answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.