Search code examples
python-2.7apipandasdataframeyahoo-finance

pandas DataFrame and Yahoo Finance API


I am trying to use the Yahoo Finance API to read data into a DataFrame. However when I read values for a symbol from a list they end up in a single column in the DataTable. I am using the API because I actually want data such as dividend, P/E and I don't think you can access these with datareader. I have two questions:

  1. How do I get values from a list to map to columns in a DataFrame (instead of rows)
  2. How would I accomplish what I am trying to do for a list for stock symbols

    import urllib2
    from pandas import DataFrame
    def get_data2(symbol):
        columns = ['last','date','change','high','low','vol']    
        url = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1c1hgv" % symbol
        file =urllib2.urlopen(url)    
        s = file.read()
        file.close()
        s= s.strip()
        L = s.split(',')
        L[0] = L[0].replace('"','')
        L[2] = L[2].replace('"','')
        D = DataFrame(L, columns=columns)
        return D
    

With this code I get a ValueError because the shapes don't match, but essentially I want to read each value from the list into a column in the DataTable, and eventually iterate over a list of symbols.

Thanks for any help


Solution

  • try this:

    In [23]: from pandas_datareader import data
    
    In [24]: data.DataReader('GOOG', 'yahoo', '2016-06-01', '2016-06-13')
    Out[24]:
                      Open        High         Low       Close   Volume   Adj Close
    Date
    2016-06-01  734.530029  737.210022  730.659973  734.150024  1250800  734.150024
    2016-06-02  732.500000  733.020020  724.169983  730.400024  1337600  730.400024
    2016-06-03  729.270020  729.489990  720.559998  722.340027  1222700  722.340027
    2016-06-06  724.909973  724.909973  714.609985  716.549988  1565300  716.549988
    2016-06-07  719.840027  721.979980  716.549988  716.650024  1336200  716.650024
    2016-06-08  723.960022  728.570007  720.580017  728.280029  1582100  728.280029
    2016-06-09  722.869995  729.539978  722.335999  728.580017   985900  728.580017
    2016-06-10  719.469971  725.890015  716.429993  719.409973  1206000  719.409973
    

    Demo for building pandas Panel when pulling data for multiple tickers

    Demo for pulling custom Yahoo quotes (for example: Market Cap, Div Yield, EPS Est Next Quarter, etc.)