StatsCan Data Reader for Python

3.2.3 · active · verified Fri Apr 17

Stats-can is a Python library designed to easily read data from Statistics Canada into pandas DataFrames. It simplifies access to the StatsCan API, allowing users to fetch specific data cubes by ID and apply filters for dimensions like geography, time, and characteristics. The current version is 3.2.3, with major releases refactoring the API for robustness, and minor releases addressing bug fixes and performance improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Statscan client and fetch data for a given Statistics Canada data cube ID. The data is returned as a pandas DataFrame. It also shows a commented example for applying basic filters.

from statscan import Statscan
import pandas as pd

# Initialize the Statscan client
sc = Statscan()

# Fetch data for a specific cube ID (e.g., '17-10-0007-01' for Consumer Price Index)
df = sc.get_data("17-10-0007-01")

# Print the first few rows of the DataFrame
print(df.head())

# You can also specify filters, e.g., for specific geographies or dates
# df_filtered = sc.get_data(
#     "17-10-0007-01",
#     filters={
#         'GEO': ['Canada', 'Ontario'],
#         'REF_DATE': ['2023-01', '2023-02']
#     }
# )
# print(df_filtered.tail())

view raw JSON →