Getting Quandl Data via Pandas Data-Reader - python

I am having trouble using pandas-datareader to import data from Quandl. Here is the code that I have tried (with a real API key):
import pandas_datareader.data as pdr
from datetime import date
start=date(1970,1,1)
end=date.today()
ticker='F'
qkey=[My API Key]
pdr.QUANDL_API_KEY=qkey
QUANDL_API_KEY=qkey
pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end)
pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end,api_key=qkey)
I get the following error messages when I run this:
>>> pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python37\lib\site-packages\pandas_datareader\data.py", line 379, in DataReader
session=session, api_key=access_key).read()
File "C:\Python37\lib\site-packages\pandas_datareader\quandl.py", line 54, in __init__
raise ValueError('The Quandl API key must be provided either '
ValueError: The Quandl API key must be provided either through the api_key variable or through the environmental variable QUANDL_API_KEY.
>>> pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end,api_key=qkey)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: DataReader() got an unexpected keyword argument 'api_key'
What am I missing? How do I provide the API key?

The source shows how the DataReader factory function passes it to the Quandl reader:
elif data_source == "quandl":
return QuandlReader(symbols=name, start=start, end=end,
retry_count=retry_count, pause=pause,
session=session, api_key=access_key).read()
So try passing it to DataReader with the access_key argument:
pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end,access_key=qkey)

A slightly different approach to quandl
import quandl
quandl.ApiConfig.api_key = "API-KEY"
aapl = quandl.get("WIKI/TICKER", start_date="YEAR-MONTH-DAY", end_date="YEAR-MONTH-DAY")

Configuration
MAC OS
VS Code
Python 3.x
Method
Run this code:
import pandas_datareader.data as pdr
api_key = [YOUR_API_KEY]
pdrquandl = pdr.DataReader("WIKI/TICKER", 'quandl', start_date, end_date, api_key)

Related

Why has pandas_datareader stopped retrieving data from IEX and Robinhood APIs

I have been using pandas_datareader to retrieve stock prices from the IEX and Robinhood APIs for some time without any hitches. However, recently (early this week), my code started failing.
Here's the code and the error:
IEX API
import pandas_datareader.data as web
import datetime as dt
end = dt.datetime.today()
start = end - relativedelta(months=20)
data = web.DataReader('MSFT', 'iex', start, end)
And the error:
web.DataReader('MSFT', 'iex', start, end)
Traceback (most recent call last):
File "<ipython-input-88-2781d4515d5c>", line 1, in <module>
web.DataReader('MSFT', 'iex', start, end)
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/data.py", line 322, in DataReader
session=session).read()
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/iex/daily.py", line 91, in read
self._get_params(self.symbols))
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/base.py", line 84, in _read_one_data
out = self._read_url_as_StringIO(url, params=params)
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/base.py", line 95, in _read_url_as_StringIO
response = self._get_response(url, params=params)
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/base.py", line 155, in _get_response
raise RemoteDataError(msg)
RemoteDataError: Unable to read URL: https://api.iextrading.com/1.0/stock/market/batch?symbols=MSFT&types=chart&range=2y
Response Text:
b'Forbidden'
Robinhood API
import pandas_datareader.data as web
import datetime as dt
end = dt.datetime.today()
start = end - relativedelta(months=20)
data = web.DataReader('MSFT', 'robinhood', start, end)
And the error:
web.DataReader('MSFT', 'robinhood', start, end)
Traceback (most recent call last):
File "<ipython-input-90-0a0fc1b0d4ce>", line 1, in <module>
web.DataReader('MSFT', 'robinhood', start, end)
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/data.py", line 391, in DataReader
session=session).read()
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/base.py", line 77, in read
return self._read_one_data(self.url, self.params)
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/base.py", line 86, in _read_one_data
out = self._get_response(url, params=params).json()
File "/anaconda3/lib/python3.7/site-packages/pandas_datareader/base.py", line 136, in _get_response
last_response_text = response.text.encode(response.encoding)
TypeError: encode() argument 1 must be str, not None
The exact code was working fine until recently. I wonder what changed, and what I can do to correct it.
Most APIs listed on the pandas_datareader documentation have been deprecated and are no longer effective through the pandas_datareader framework (Yahoo, Google, etc).
However, Tiingo works just fine, at least for now.
To use Tiingo API, you'll first need to sign up for a free account (or paid if you want premium features). You can do that here. After activating your Tiingo account, you'll receive an API key which you can use to retrieve stock historical data with pandas_datareader (find details here).
From https://iextrading.com/developer/docs/#stocks
"On June 1, 2019, IEX Group removed all non-IEX data, and certain functionality, according to the schedule. IEX Cloud, a non-Exchange platform, will continue to provide access to third-party data sources."
Looks like you'll need access and secret keys to call their API. I'm looking into this for my python app stock_quote that hit this issue.
You can use the efficient Python library for Yahoo Finance, yfinance (GitHub link).
You can also find example codes here and here.

Unable to define an output file with XlsxWriter in Python

I now have the simplest test script I can imagine, but it won’t work according to the XlsxWriter instructions:
import pandas as pd
import numpy as np
import xlsxwriter
writer = pd.ExcelWriter("Test.xlsx", engine = "xlsxwriter")
Full error:
Traceback (most recent call last):
File "<ipython-input-310-9c7e424c6d39>", line 1, in <module>
writer = pd.ExcelWriter("Test.xlsx", engine = "xlsxwriter")
File "/Users/ernie/anaconda3/lib/python3.6/site- packages/pandas/io/excel.py", line 1725, in __init__
self.book = xlsxwriter.Workbook(path, **engine_kwargs)
File "/Users/ernie/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py", line 68, in __init__
super(Workbook, self).__init__()
TypeError: super(type, obj): obj must be an instance or subtype of type
This error apparently had something to do with the session where Spyder and Python were running. I exited the session completely, re initiated it, and the error disappeared. Something somehow got corrupted, but very strange.

AttributeError: '_process_plot_var_args' object has no attribute 'get_next_color'

I have tried to run the following code. but it gives an argument required error in lifelines/plotting.py file. i can't fix it .
import pandas as pd
from lifelines.datasets import load_dd
import matplotlib.pyplot as plt
data = load_dd()
print data.sample(6)
from lifelines import KaplanMeierFitter
kmf = KaplanMeierFitter()
T = data["duration"]
E = data["observed"]
kmf.fit(T, event_observed=E)
kmf.survival_function_.plot()
plt.title('Survival function of political regimes');
kmf.plot()
plt.show()
but it gives the following error
Traceback (most recent call last): File "/Users/rabindra/PycharmProjects/SurvivalAnalysis/sources/main.py", line 17, in <module>
kmf.plot() File "/Library/Python/2.7/site-packages/lifelines/plotting.py", line 331, in plot
set_kwargs_color(kwargs) File "/Library/Python/2.7/site-packages/lifelines/plotting.py", line 223, in set_kwargs_color
kwargs["ax"]._get_lines.get_next_color()) AttributeError: '_process_plot_var_args' object has no attribute 'get_next_color'
I was facing the same issue.
Upgraded lifelines to 0.14.0 and matplotlib to 2.2.2 and it works.

My code using Pandas Options did work - now throws an error

My previously working code now throws an error. So I have now tried the code given in the Docs 0.15.1 for Options and this throws a similar error.
from pandas.io.data import Options
aapl = Options('aapl', 'yahoo')
data = aapl.get_all_data()
Traceback (most recent call last):
File "<ipython-input-10-a2dc6d91ec5f>", line 1, in <module>
data = aapl.get_all_data()
File "C:\Users\Ray\Anaconda\lib\site-packages\pandas\io\data.py", line 1111, in get_all_data
m2 = month.month
Error:
AttributeError: 'str' object has no attribute 'month'

Python: Very simple strptime() call throws error

Some very simple code leads to a mystifying error, and Google gives me nothing...
Code:
import sys
import datetime
for line in sys.stdin:
date = datetime.datetime.strptime(line, '%Y%m%dT%H%M%S')
print date
First few input lines on stdin:
20101119T141500
20101119T164500
20110310T081500
20110310T113000
20100218T113000
...
Error:
Traceback (most recent call last):
File "time.py", line 5, in <module>
date = datetime.datetime.strptime(line, '%Y%m%dT%H%M%S')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 13, in <module>
import time
File "<path>", line 5, in <module>
date = datetime.datetime.strptime(line, '%Y%m%dT%H%M%S')
AttributeError: _strptime
Platform: Mac OS X 10.7.3, Python 2.7.1
You called your script "time.py", shadowing the time module. Don't do that.

Categories