census module provides utilities for retrieving demographic data and geographic boundaries from the US Census Bureau API, including block group enrichment for property valuation models.
Service Classes
CensusCredentials
Object for storing US Census API credentials.API Key for the US Census
CensusService
Provides functions for downloading data from the US Census.Credentials for the US Census
Census Data Retrieval
get_census_data()
Get Census demographic data for block groups in a given FIPS code.5-digit FIPS code (state + county)
Census year to query
Census settings dictionary
DataFrame containing Census demographic data with columns:
median_income: Median household incometotal_population: Total populationmedian_g_rent: Median gross rentmedian_c_rent: Median contract rentstd_geoid: Standardized GEOID (state + county + tract + block group)- Various FIPS component fields
get_census_blockgroups_shapefile()
Get Census Block Group shapefiles for a given FIPS code from the Census TIGERweb service.5-digit FIPS code (state + county)
GeoDataFrame containing Census Block Group boundaries with:
geometry: Polygon geometries in EPSG:4326std_geoid: Standardized GEOID- FIPS component fields (state_fips, county_fips, tract_fips, bg_fips)
get_census_data_with_boundaries()
Get both Census demographic data and boundary files for block groups in a FIPS code.5-digit FIPS code (state + county)
Census year to query
Census settings object
Tuple containing:
- Census demographic data DataFrame
- Census Block Group boundaries GeoDataFrame (merged with demographic data)
Spatial Matching
match_to_census_blockgroups()
Match each row in a GeoDataFrame to its corresponding Census Block Group using spatial join.Input GeoDataFrame to match (must have a
geometry column)Census Block Group boundaries GeoDataFrame
Type of join to perform:
'left', 'right', 'inner', or 'outer'Input GeoDataFrame with Census Block Group data appended, including:
std_geoid: Census block group IDmedian_income: Median household incometotal_population: Total population- Other demographic fields from the census data
Initialization Functions
init_service_census()
Initialize a Census service with the provided credentials.Census API credentials
Initialized Census service
get_creds_from_env_census()
Get Census credentials from environment variables.Census API credentials loaded from the
CENSUS_API_KEY environment variable. Returns None with a warning if the environment variable is not set.