Search code examples
pythonpandasdatareader

Allow user selection to determine the stock selection in web.DataReader


I'm looking to create a program that will take a user selected stock and return information using the web.DataReader function in Pandas. Any suggestions or alternative solutions would be appreciated.

import pandas as pd
import pandas.io.data as web   # Package and modules for importing data; this code may change depending on pandas version
import datetime

start = datetime.datetime(2016,1,1)
end = datetime.date.today()

apple = web.DataReader(input(""), "yahoo", start, end)

type(apple)
apple.head()

Results with

input("") in web.DataReader statement

OSError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=appl&a=0&b=1&c=2016&d=11&e=8&f=2016&g=d&ignore=.csv'


Solution

  • Here's how you should really do it now that the data readers have been moved into their own package. You also need to provide a valid ticker, in case of apple it's AAPL and not appl

    import pandas as pd
    from pandas_datareader import data, wb
    import datetime
    
    start = datetime.datetime(2016,1,1)
    end = datetime.date.today()
    
    apple = data.DataReader(input("Please Input the name of the Ticker:\n"), "yahoo", start, end)
    
    type(apple)
    apple.head()
    
    Please Input the name of the Ticker:
    AAPL
    
        Open    High    Low     Close   Volume  Adj Close
    Date                        
    2016-01-04  102.610001  105.370003  102.000000  105.349998  67649400    103.057063
    2016-01-05  105.750000  105.849998  102.410004  102.709999  55791000    100.474523
    2016-01-06  100.559998  102.370003  99.870003   100.699997  68457400    98.508268
    2016-01-07  98.680000   100.129997  96.430000   96.449997   81094400    94.350769
    2016-01-08  98.550003   99.110001   96.760002   96.959999   70798000    94.849671