Skip to content

Instantly share code, notes, and snippets.

@rruntsch
Last active April 14, 2022 16:29
Show Gist options
  • Save rruntsch/b5a9d0bb3804101e584a9771713ea869 to your computer and use it in GitHub Desktop.
Save rruntsch/b5a9d0bb3804101e584a9771713ea869 to your computer and use it in GitHub Desktop.
Python program to call the U.S. Bureau of Labor Statistics (BLS) Public Data API to retrieve economic data and write it to a CSV file.
#
# Name: c_bls_data.py
# Date: April 14, 2022
# Author: Randy Runtsch
#
# Description:
#
# The c_bls_data class uses Version 2 of the US Bureau of Labor Statistics (BLS) API
# to obtain one of more series of BLS data. It retrieves the data in
# JavaScript Object Notation (JSON) format, converts the data to CSV records,
# and writes those records to an output CSV file.
#
# The class can retrieve monthly data for one or more series for up to 20 years.
#
# To use this class, each use must provide their own BLS API Version 2
# registration key from here: https://data.bls.gov/registrationEngine/
#
# To use the c_bls_data class, create an instance with these inputs:
#
# 1. BLS API registration key.
# 2. Full path of file where program will write the output data in CSV form.
# 2. List of BLS data series IDs.
# 3. Start year of data.
# 4. End year of data.
#
import requests
import json
import csv
class c_bls_data:
def __init__(self, reg_key, out_file_nm, series_id, start_year, end_year):
# Set the file name variable and create the parameters for the API request.
self.out_file_nm = out_file_nm
headers = {'Content-type': 'application/json'}
parameters = json.dumps({'seriesid' : series_id, 'startyear' : start_year, 'endyear' : end_year, 'calculations' : True , 'registrationkey' : reg_key})
# Get data in JSON format and then write it to a CSV file.
json_data = self.get_data(headers, parameters)
self.write_data_to_csv(json_data)
def get_data(self, headers, parameters):
# Post the data request to the BLS API. Return the resulting JSON structure.
post = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data = parameters, headers = headers)
json_data = json.loads(post.text)
return json_data
def write_data_to_csv(self, json_data):
# Convert the data from JSON format to CSV records. Write
# each record to the specified output file.
# Open the output file. Then, set up the field names for the CSV records and set up the CSV writer.
with open(self.out_file_nm, mode = 'w', newline = '') as data_file:
fieldnames = ['Series ID', 'Month', 'Value', 'Annual Percent Change']
data_writer = csv.writer(data_file, delimiter = ',', quotechar = '"', quoting = csv.QUOTE_ALL)
# Write CSV file header.
data_writer.writerow(fieldnames)
# Write each record to the output file.
for series in json_data['Results']['series']:
series_id = series['seriesID']
for item in series['data']:
# Get the basic data
year = item['year']
period_name = item['periodName']
value = item['value']
# Get the 12-month change
calculations = item['calculations']
pct_changes = calculations['pct_changes']
annual_pct_chg = pct_changes['12']
# Create a month field in the format of a date for
# the first day of each month (for example: January 1, 2022).
month = period_name + ' 1, ' + year
#Write the CSV record to the output file.
data_writer.writerow([series_id, month, value, annual_pct_chg])
#
# Name: run_get_bls_data.py
# Date: April 14, 2022
# Author: Randy Runtsch
#
# Description:
#
# Use the c_bls_data class to obtain series of data
# from the US Bureau of Labor Statistics (BLS) API.
from c_bls_data import c_bls_data
#
# See the code in c_bls_data.py for descriptions of the class's input parameters.
#
# Create and instance of c_bls_data to retrieve cpi data for 1) all items and 2) regular gasoline from the years 2003 through 2022.
# Requested BLS data series IDs and titles:
#
# CUSR0000SA0 - All items in U.S. city average, all urban consumers, seasonally adjusted
# CUSR0000SETB01 - Gasoline (all types) in U.S. city average, all urban consumers, seasonally adjusted
# CUSR0000SAF1 - Food in U.S. city average, all urban consumers, seasonally adjusted
# CUSR0000SETA02 - Used cars and trucks in U.S. city average, all urban consumers, seasonally adjusted
#
c_bls_data('your_registration_key_here', 'c:/bls_data/inflation.csv', ['CUSR0000SA0', 'CUSR0000SETB01', 'CUSR0000SAF1', 'CUSR0000SETA02'], 2003, 2022)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment