Census API

Updated · ■ Beginner ·

U.S. Census Bureau Application Programming Interface with Python

The Census Bureau API provides access to a wide range of economic indicators, including manufacturing, trade, and business data. This tutorial demonstrates how to retrieve data from the M3 Manufacturers' Shipments, Inventories, and Orders survey using Python.

The example below retrieves the value of new orders for nondefense capital goods excluding aircraft. This serves as a proxy for new investment in business equipment and is a closely watched indicator of business investment trends.


Background

Census Bureau

The U.S. Census Bureau collects data on manufacturing activity through several surveys. The M3 survey provides monthly estimates of shipments, inventories, and orders for domestic manufacturing establishments.

API Documentation

Information on what is required to make the API call is available in the variables documentation. The list of individual series codes is in the code list PDF.

Python

This example uses Python 3 and the requests and pandas packages.


Example: Nondefense Capital Goods Orders

Setup

The Census API key is stored locally in a file called config.py (see the Getting Started guide for details). You can register for a free API key at the Census Bureau website.

In[1]:

import requests
import pandas as pd
import config
key = config.census_key
Request Data from the API

The Census API takes query parameters including the fields to return (get), a category code, data type, and time range. Here, NXA is nondefense capital goods excluding aircraft, and VS is the seasonally adjusted value. Data are retrieved from 2022 onward.

In[2]:

url = 'https://api.census.gov/data/timeseries/eits/advm3'
params = {
    'get': 'cell_value,time_slot_id',
    'key': key,
    'category_code': 'NXA',   # Nondefense capital goods ex. aircraft
    'data_type_code': 'VS',   # Seasonally adjusted value
    'time': 'from 2022',
    'for': 'us',
    'seasonally_adj': 'yes'
}

r = requests.get(url, params=params).json()
Convert to Pandas DataFrame

The Census API returns JSON as a list of lists, where the first row contains column headers. We parse this into a DataFrame and set up a datetime index.

In[3]:

df = pd.DataFrame(r[1:], columns=r[0])
df.index = pd.to_datetime(df['time'] + '-01')
df.index.name = ''
df = df.sort_index()
df['cell_value'] = df['cell_value'].astype(float)
Calculate Growth Rate

The pct_change(12) method compares each month to the same month one year earlier, giving us a year-over-year growth rate that avoids seasonal distortions.

In[4]:

growth = df['cell_value'].pct_change(12) * 100
Plot the Results

Plot the 12-month percent change from 2024 onward.

In[5]:

title = ('New Orders: Nondefense capital goods excluding aircraft,\n'
         '12-month percent change')
ax = growth.loc['2024':].plot(title=title)
ax.axhline(0, lw=0.5, color='gray', zorder=0)

Out[5]:

Census M3 New Orders Growth Rate

Further Resources