Python bindings for the Statistics Canada Web Data Service (WDS) API
Project description
Statistics Canada Python Bindings
Python bindings for the Statistics Canada Web Data Service (WDS) API, providing easy access to Canadian census data and geographic information.
Overview
This package provides a Python interface to Statistics Canada's census data through their Web Data Service API. It includes utilities for downloading, processing, and working with Canadian census data, as well as geographic boundaries and administrative divisions.
Key Features:
- Access to Canadian census data from 1976-2021
- Geographic level enumerations (provinces, census divisions, etc.)
- Automated data download utilities
- DGUID (Dissemination Geography Unique Identifier) support
- Integration with pandas for data analysis
Data Sources:
Installation
From PyPI (when available)
pip install statistics-canada
From source
git clone https://github.com/pbouill/statistics-canada.git
cd statistics-canada
pip install -e .
Usage
Basic Usage
import statscan
from statscan.census import CensusYear
from statscan.enums.vintage import Vintage
from statscan.enums.schema import Schema
from statscan.enums.auto.province_territory import ProvinceTerritory
# Access census data for different years
census_year = CensusYear.CENSUS_2021
vintage = Vintage.CENSUS_2021
# Work with geographic levels
province = ProvinceTerritory.ONTARIO
print(f"Province: {province.name}, Code: {province.value}")
# Work with geographic schemas
geo_level = Schema.PR # Province level
print(f"Geographic level: {geo_level.value}")
# Download and work with data
from statscan.util.data import download_data, unpack_to_dataframe
import asyncio
async def get_census_data():
# Download geographic attribute files
data_path = await download_data(
"https://www12.statcan.gc.ca/census-recensement/2021/geo/aip-pia/attribute-attribs/files-fichiers/2021_92-151_X.zip"
)
print(f"Data downloaded to: {data_path}")
# Unpack to DataFrame
df = unpack_to_dataframe(data_path)
print(f"Data shape: {df.shape}")
# Run the async function
asyncio.run(get_census_data())
Available Census Years
The package supports census data from:
- 2021 (latest and currently supported)
- Additional years (2016, 2011, 2006, 2001, 1996, 1991, 1986, 1981, 1976) available through legacy CensusYear enum
Geographic Levels
The package includes enumerations for various Canadian geographic divisions:
- Schema: Geographic level codes (provinces, census divisions, etc.)
- ProvinceTerritory: Provinces and territories with official codes
- CensusDivision: Census division codes
- CensusSubdivision: Census subdivision codes
- FederalElectoralDistrict: Federal electoral district codes
- CensusMetropolitanArea: CMA codes
- EconomicRegion: Economic region codes
- And more auto-generated geographic enumerations...
Development
Requirements
- Python 3.11 or later
- httpx >= 0.28.1
- pandas >= 2.3.0
Setup development environment
# Clone the repository
git clone https://github.com/pbouill/statistics-canada.git
cd statistics-canada
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install development dependencies
pip install -r requirements.dev.txt
# Install package in editable mode
pip install -e .
Running Tests
# Run all tests
python -m unittest discover unittests
# Run specific test module
python -m unittest unittests.test_core
Project Structure
statscan/ # Main package
├── __init__.py # Package initialization
├── _version.py # Version information
├── census.py # Legacy census year enumerations
├── dguid.py # DGUID utilities
├── url.py # API URLs and endpoints
├── py.typed # Type hint marker
├── enums/ # Geographic enumerations
│ ├── schema.py # Geographic level schema definitions
│ ├── vintage.py # Current census vintage/year
│ ├── frequency.py # Data frequency enumerations
│ ├── auto/ # Auto-generated enums
│ │ ├── province_territory.py # Province/territory codes
│ │ ├── census_division.py # Census division codes
│ │ ├── census_subdivision.py # Census subdivision codes
│ │ ├── federal_electoral_district.py # FED codes
│ │ ├── census_metropolitan_area.py # CMA codes
│ │ ├── economic_region.py # ER codes
│ │ └── ... # Other geographic levels
│ └── geocode/ # Geocoding utilities
│ ├── geocode.py # Base geocode classes
│ ├── pr_geocode.py # Province-specific geocoding
│ └── ...
└── util/ # Utility modules
├── data.py # Data download and processing utilities
└── log.py # Logging configuration
API Reference
Census Data Access
- CensusYear: Legacy enumeration of supported census years (1976-2021)
- Vintage: Current census vintage (currently 2021)
- WDS_BASE_URL: Base URL for Statistics Canada Web Data Service
Geographic Enumerations
- Schema: Geographic level schema codes (CAN, PR, CD, CSD, etc.)
- ProvinceTerritory: Canadian provinces and territories with official codes
- CensusDivision: Census division codes by province
- CensusSubdivision: Census subdivision codes
- FederalElectoralDistrict: Federal electoral district codes
- CensusMetropolitanArea: Census metropolitan area codes
- EconomicRegion: Economic region codes
Data Utilities
- download_data(): Async function to download data files from Statistics Canada URLs
- unpack_to_dataframe(): Function to unpack downloaded files into pandas DataFrames
- DGUID utilities: Functions for working with Dissemination Geography Unique Identifiers
Data Sources and References
This package provides access to official Statistics Canada data:
- Web Data Service (WDS) API
- Census Geographic Attribute Files
- Standard Geographical Classification (SGC)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file statistics_canada-2025.6.12.201824.tar.gz.
File metadata
- Download URL: statistics_canada-2025.6.12.201824.tar.gz
- Upload date:
- Size: 236.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29da2850831569bc0c845b3e3fb41f9a4ecd70bdeeeb0bd6fd920932fd5913b5
|
|
| MD5 |
f423dbc0408311a49bd8c8d10cce346b
|
|
| BLAKE2b-256 |
98aa251be1651f2cab0c09ec12fe30fc946a4eccb92a0a83bc46749ac274a040
|
Provenance
The following attestation bundles were made for statistics_canada-2025.6.12.201824.tar.gz:
Publisher:
publish.yml on pbouill/statistics-canada
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
statistics_canada-2025.6.12.201824.tar.gz -
Subject digest:
29da2850831569bc0c845b3e3fb41f9a4ecd70bdeeeb0bd6fd920932fd5913b5 - Sigstore transparency entry: 237210130
- Sigstore integration time:
-
Permalink:
pbouill/statistics-canada@f2238425d6567b266582c2cc5d73869bf0e4963a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/pbouill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f2238425d6567b266582c2cc5d73869bf0e4963a -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file statistics_canada-2025.6.12.201824-py3-none-any.whl.
File metadata
- Download URL: statistics_canada-2025.6.12.201824-py3-none-any.whl
- Upload date:
- Size: 244.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
856ab1338f986ce236e59114b690cc2fb72ccac4df2f58c7f0f5c4ed6f5e2493
|
|
| MD5 |
4cca9172ea76a1e9ec494a39c02b7eb3
|
|
| BLAKE2b-256 |
4136daa7a0a150d53da9098d8b066fe0af57a8bb7c5e8ee080e13fd885232007
|
Provenance
The following attestation bundles were made for statistics_canada-2025.6.12.201824-py3-none-any.whl:
Publisher:
publish.yml on pbouill/statistics-canada
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
statistics_canada-2025.6.12.201824-py3-none-any.whl -
Subject digest:
856ab1338f986ce236e59114b690cc2fb72ccac4df2f58c7f0f5c4ed6f5e2493 - Sigstore transparency entry: 237210131
- Sigstore integration time:
-
Permalink:
pbouill/statistics-canada@f2238425d6567b266582c2cc5d73869bf0e4963a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/pbouill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f2238425d6567b266582c2cc5d73869bf0e4963a -
Trigger Event:
workflow_run
-
Statement type: