Matus
Matus

Reputation: 43

Work with data in python and numpy/pandas

so I started learning how to work with data in python. I wanted to load multiple securities. But I have an error that I can not fix for some reason. Could someone tell me what is the problem?

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt

tickers = ['PG', 'MSFT', 'F', 'GE']
mydata = pd.DataFrame()
for t in tickers:
    mydata[t] = wb.DataReader(t, data_source='yahoo', start = '1955-1-1')

enter image description here

Upvotes: 0

Views: 198

Answers (2)

Poe Dator
Poe Dator

Reputation: 4913

you need 2 fixes here: 1) 1955 is too early for this data source, try 1971 or later. 2) your data from wb.DataReader(t, data_source='yahoo', start = '1971-1-1') comes as dataframe with multiple series, so you can not save it to mydata[t] as single series. Use a dictionary as in the other answer or save only closing prices: mydata[t] = pdr.data.DataReader(t, data_source='yahoo', start = '2010-1-1')['Close']

Upvotes: 1

FAHAD SIDDIQUI
FAHAD SIDDIQUI

Reputation: 641

First of all please do not share information as images unless absolutely necessary. See: this link Now here is a solution to your problem. You are using year '1955' but there is a possibility that data is not available for this year or there may be some other issues. But when you select the right year it will work. Another thing it returns data as dataframe so you can not assign it like a dictionary so instead of making a DataFram you should make a dictionary and store all dataframes into it.

Here is improved code choose year carefully

import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt

from datetime import datetime as dt

tickers = ['PG', 'MSFT', 'F', 'GE']
mydata = {}
for t in tickers:
    mydata[t] = wb.DataReader(t, data_source='yahoo',start=dt(2019, 1, 1), end=dt.now())

Output

mydata['PG']

               High        Low         Open        Close      Volume    Adj Close
Date                        
2018-12-31  92.180000   91.150002   91.629997   91.919998   7239500.0   88.877655
2019-01-02  91.389999   89.930000   91.029999   91.279999   9843900.0   88.258835
2019-01-03  92.500000   90.379997   90.940002   90.639999   9820200.0   87.640022
2019-01-04  92.489998   90.370003   90.839996   92.489998   10565700.0  89.428787

Upvotes: 1

Related Questions