In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
import quandl

Make a Basic Data Call

This call gets the WTI Crude Oil price from the US Department of Energy:


In [3]:
mydata = quandl.get("EIA/PET_RWTC_D")

In [4]:
mydata.head()


Out[4]:
Value
Date
1986-01-02 25.56
1986-01-03 26.00
1986-01-06 26.53
1986-01-07 25.85
1986-01-08 25.87

In [5]:
mydata.plot(figsize = (12, 6))


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x29cac2c7f98>

Note that you need to know the "Quandl code" of each dataset you download. In the above example, it is "EIA/PET_RWTC_D".

Change Formats

You can get the same data in a NumPy array:


In [6]:
mydata = quandl.get("EIA/PET_RWTC_D", 
                    returns = "numpy")

Specifying Data

To set start and end dates:


In [7]:
mydata = quandl.get("FRED/GDP", 
                    start_date = "2001-12-31", 
                    end_date = "2005-12-31")

In [8]:
mydata.head()


Out[8]:
Value
Date
2002-01-01 10834.4
2002-04-01 10934.8
2002-07-01 11037.1
2002-10-01 11103.8
2003-01-01 11230.1

In [9]:
mydata = quandl.get(["NSE/OIL.1", "WIKI/AAPL.4"])

In [10]:
mydata.head()


Out[10]:
NSE/OIL - Open WIKI/AAPL - Close
Date
1980-12-12 NaN 28.75
1980-12-15 NaN 27.25
1980-12-16 NaN 25.25
1980-12-17 NaN 25.87
1980-12-18 NaN 26.63

Usage Limits

The Quandl Python module is free. If you would like to make more than 50 calls a day, however, you will need to create a free Quandl account and set your API key:


In [11]:
# EXAMPLE
quandl.ApiConfig.api_key = "2qM_u-g8oxTV6JbhUWLn"
mydata = quandl.get("FRED/GDP")

Database Codes

Each database on Quandl has a short (3-to-6 character) database ID. For example:

  • CFTC Commitment of Traders Data: CFTC
  • Core US Stock Fundamentals: SF1
  • Federal Reserve Economic Data: FRED

Each database contains many datasets. Datasets have their own IDs which are appended to their parent database ID, like this:

  • Commitment of traders for wheat: CFTC/W_F_ALL
  • Market capitalization for Apple: SF1/AAPL_MARKETCAP
  • US civilian unemployment rate: FRED/UNRATE

You can download all dataset codes in a database in a single API call, by appending /codes to your database request. The call will return a ZIP file containing a CSV.

Databases

Every Quandl code has 2 parts: the database code (“WIKI”) which specifies where the data comes from, and the dataset code (“FB”) which identifies the specific time series you want.

You can find Quandl codes on their website, using their data browser.

https://www.quandl.com/search


In [12]:
# FOR STOCKS

In [13]:
mydata = quandl.get('WIKI/FB',
                    start_date = '2015-01-01',
                    end_date = '2017-01-01')

In [14]:
mydata.head()


Out[14]:
Open High Low Close Volume Ex-Dividend Split Ratio Adj. Open Adj. High Adj. Low Adj. Close Adj. Volume
Date
2015-01-02 78.58 78.9300 77.700 78.450 18177475.0 0.0 1.0 78.58 78.9300 77.700 78.450 18177475.0
2015-01-05 77.98 79.2455 76.860 77.190 26452191.0 0.0 1.0 77.98 79.2455 76.860 77.190 26452191.0
2015-01-06 77.23 77.5900 75.365 76.150 27399288.0 0.0 1.0 77.23 77.5900 75.365 76.150 27399288.0
2015-01-07 76.76 77.3600 75.820 76.150 22045333.0 0.0 1.0 76.76 77.3600 75.820 76.150 22045333.0
2015-01-08 76.74 78.2300 76.080 78.175 23960953.0 0.0 1.0 76.74 78.2300 76.080 78.175 23960953.0

In [15]:
mydata = quandl.get('WIKI/FB.1',
                    start_date = '2015-01-01',
                    end_date = '2017-01-01')

In [16]:
mydata.head()


Out[16]:
Open
Date
2015-01-02 78.58
2015-01-05 77.98
2015-01-06 77.23
2015-01-07 76.76
2015-01-08 76.74

In [17]:
mydata = quandl.get('WIKI/FB.7',
                    start_date = '2015-01-01',
                    end_date = '2017-01-01')

In [18]:
mydata.head()


Out[18]:
Split Ratio
Date
2015-01-02 1.0
2015-01-05 1.0
2015-01-06 1.0
2015-01-07 1.0
2015-01-08 1.0

Housing Price Example

Zillow Home Value Index (Metro): Zillow Rental Index - All Homes - San Francisco, CA

The Zillow Home Value Index is Zillow's estimate of the median market value of zillow rental index - all homes within the metro of San Francisco, CA. This data is calculated by Zillow Real Estate Research (www.zillow.com/research) using their database of 110 million homes.


In [19]:
houses = quandl.get('ZILLOW/M11_ZRIAH')

In [20]:
houses.head()


Out[20]:
Value
Date
2010-11-30 2241.0
2010-12-31 2254.0
2011-01-31 2276.0
2011-02-28 2305.0
2011-03-31 2334.0

In [21]:
houses.plot(figsize = (12, 6))


Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x29cad90a518>