Inflation in the Car Market (2/8)

Downloading Data in Callisto Jupyter Notebooks

In Part 1 we laid out the motivation for this series – the ridiculously inflated car prices! We also established the overarching goals of our data science project in Callisto Notebooks.

Using the FRED API

To explore economic data, we will first need to get our (digital) hands on it.

FRED has done an excellent job providing regular users like you and me an interface to search and download data in our Python Notebooks. We will be using the FRED API. The details for the API don’t matter too much for our purposes. All we need to know right now is that the FRED API is a list of functions that we can use to get our data.

0. Before Starting: Setup

Make sure you have the latest version of Callisto (Callisto -> Check For Updates). Then open up a new Python Notebook from the app’s homepage (New -> Local -> Python Kernel -> Save in a new folder — call it “Car Prices Project”)

After the notebook is open, check the working directory of the notebook on the File Browser in the left column. In the video, the notebook is in the “Car Prices Project” folder. This means any files that are in that folder will show up in the notebook window’s left column.   This step is not strictly necessary but it will make it easier to follow the tutorial — especially when it comes to importing data from a file by name.

To change the working directory click the “…” 1 This can be found in the File browser column on the left side of the notebook window. Beside “Files” and choose “Change Working Directory”.

1. Gather your tools!

Let’s install and import the necessary Python Libraries.

Note: Callisto already comes with `Pandas` and `Seaborn` so installing those may not be necessary.

To install:

%pip install -Uqq pandas
%pip install -Uqq seaborn
%pip install -Uqq fredapi
2. Get your tools primed for use!

To get everything ready to work, we will import the libraries and initiate our objects.

Run the following code:

# import the libraries
import pandas as pd
import requests
import seaborn as sns
from fredapi import Fred

# set seaborn chart styles
sns.set_style("darkgrid")
sns.set(font_scale=1.5)
import matplotlib.pyplot as plt
from IPython.core.pylabtools import figsize
%matplotlib inline
figsize(20, 5)
3. Get the key to access FRED API

You will need an API Key from FRED. This key is necessary and you’ll need to access the FRED data. Once you have the key, load it in your Fred object.

# paste it directly into Fred(api_key=your_key)
fred = Fred(api_key="[YOUR_API_KEY]")
4. Search for Used Car Pricing data we can download

Here’s the low down on how the FRED API works. It has access to thousands of different types of economic data that you can download. To narrow down, we need to search for what we want. Once we settle on that, we grab the ID of that data and download data for that ID.

We’ll do this in two parts.

First: Search for data available relating to “car” which will give many entries in return. We will look through the descriptions and find one we like and get its ID.

Second: Hand that ID off to the `fred` object and download our data.

To search for relevant data in FRED, run this code in a new cell:

# code to search
auto_search = fred.search("car used")

Once that’s finished, go over to the Data Explorer and look for `car_search`. Click on the detail icon “i” and scroll horizontally until you see the Title column. Read the description for each to familiarize yourself. The series we want to look at for Used Car prices is the “Used Car Price Index” and we want the one that is seasonally adjusted. How to check for that? Look for that in the “Seasonal_Adjustment” column.

Here’s the code that already found the ID for the Used Car Price Index

# code to get the ID for the Used Car Price Index
cpi_used_car_id = auto_search["id"][0]
print(cpi_used_car_id)
5. Download Used Car Pricing data

This is simple now. Run the following to get the latest data for the Used Car Price Index

cpi_used_car = fred.get_series_latest_release(cpi_used_car_id)

Again, go over to the Data Explorer and look at the details of cpi_used_car. You should see many many values timestamped from 1954 to the present year.

What’s Next?

Plot the Used Car Prices!

In the next article, we will get to the fun part. Charting the data we downloaded! Check out the next article to see how easy it is to make graphs in Callisto.