Search code examples
pythonpandasquandlpandas-datareader

How to grab stock data from Quandl with tickers that have a full stop? Python


Due to Python's pandas_datareader API no longer being able to extract Yahoo finance stock data, I'm using quandl. This requires setting up an account and doing a 'pip install quandl' in the command terminal. If I have an unusual stock ticker, like say BRK.B, using the pandas_datareader API to extract stock data from Google works perfectly fine.

 import pandas as pd
 import datetime
 from pandas_datareader import data, wb
 start = datetime.datetime(2016, 1, 1)
 end = datetime.datetime(2017, 1, 1)
 brk = data.DataReader("BRK.B", "google", start, end)["Close"]
 brk

However, this won't work with quandl due to the stock ticker, BRK.B having a full-stop inside it.

 import quandl
 brk = ["BRK.B"]
 for stk in brk:
     b = quandl.get("WIKI/{}".format(stk),
         authtoken = "Mixture of numbers, and lower/upper case letters",
         start, end)["Adj. Close"]

This causes the code to break. However, if I swap ["BRK.B"] for any normal stock ticker, say ["AAPL"], it works perfectly fine. I'd like to extract all 505 stock from the S&P 500 using the quandl API, however, because some stock tickers have a "." in them, it won't work. I've tried format(stk.replace(".", "-")) and that didn't work either.

Any help would be greatly appreciated. BTW, for those who don't know. You have to set up a quandl account, then get your API key code from your quandl account settings, then copy and paste it as your authtoken.


Solution

  • Did you try to replace it with underscore maybe?

    "WIKI/{}".format(stk.replace(".", "_"))  # replacing . with _ instead of -
    

    If I remember correctly, dot is used to slice the specific column from data.