import morpc
morpc.census¶
MORPC works regularly with census data, including but not limited to ACS 5 and 1-year, Decennial Census, PEP, and geographies. The following module is useful for gathering and organizing census data for processes in various workflow. Those workflows are linked when appropriate.
ACS functions and variables¶
acs_get() is a low-level wrapper for Census API requests that returns the results as a pandas dataframe. If necessary, it splits the request into several smaller requests to bypass the 50-variable limit imposed by the API.
The resulting dataframe is indexed by GEOID (regardless of whether it was requested) and omits other fields that are not requested but which are returned automatically with each API request (e.g. “state”, “county”)
url = 'https://api.census.gov/data/2022/acs/acs1'
params = {
"get": "GEO_ID,NAME,B01001_001E",
"for": "county:049,041",
"in": "state:39"
}
acs = morpc.census.acs_get(url, params)
Total variables requested: 3
Starting request #1. 3 variables remain.
acs
Using morpc-censusacs-fetch as an input¶
When using ACS data, generally we will be digesting data produded using the morpc
Run that script according to the documentation and then use acs_generate_dimension_table() downstream.
Load the data using frictionless.load_data()¶
data, resource, schema = morpc.frictionless.load_data('../../morpc-censusacs-fetch/output_data/morpc-acs5-2023-us-B01001.resource.yaml', verbose=False)
morpc.load_data | INFO | Loading Frictionless Resource file at location ..\..\morpc-censusacs-fetch\output_data\morpc-acs5-2023-us-B01001.resource.yaml
morpc.load_data | INFO | Loading data, resource file, and schema from their source locations
morpc.load_data | INFO | --> Data file: ..\..\morpc-censusacs-fetch\output_data\morpc-acs5-2023-us-B01001.csv
morpc.load_data | INFO | --> Resource file: ..\..\morpc-censusacs-fetch\output_data\morpc-acs5-2023-us-B01001.resource.yaml
morpc.load_data | INFO | --> Schema file: ..\..\morpc-censusacs-fetch\output_data\morpc-acs5-2023-us-B01001.schema.yaml
morpc.load_data | INFO | Loading data.
data
Using ACS_ID_FIELDS to get the fields ids¶
idFields = [field["name"] for field in morpc.census.ACS_ID_FIELDS['us']]
morpc.census.acs_generate_universe_table(data.set_index("GEO_ID"), "B01001_001")
Create a dimension table with the data and the dimension names¶
dim_table = morpc.census.acs_generate_dimension_table(data.set_index("GEO_ID"), schema, idFields=idFields, dimensionNames=["Sex", "Age group"])
dim_table.loc[dim_table['Variable type'] == 'Estimate'].head()