pytidycensus.utils
Utility functions for data processing and validation.
Functions
|
Add margin of error columns for ACS data with confidence level adjustment. |
|
Add NAME column using national_county.txt lookup table for geographic areas. |
|
Build geography parameters for Census API call. |
|
Check for and warn about overlapping ACS periods. |
Load county lookup table from national_county.txt. |
|
|
Look up county FIPS code by name. |
|
Process raw Census API response into pandas DataFrame. |
|
Validate and convert county identifiers to FIPS codes. |
|
Validate geography parameter. |
|
Validate and convert state identifiers to FIPS codes. |
|
Validate year for given dataset. |
- pytidycensus.utils.load_county_lookup()[source]
Load county lookup table from national_county.txt.
- Returns:
DataFrame with columns: state_abbrev, state_fips, county_fips, county_name
- Return type:
pd.DataFrame
- pytidycensus.utils.add_name_column(df)[source]
Add NAME column using national_county.txt lookup table for geographic areas.
Works for state, county, and tract level geographies by matching GEOID. For tract-level data, shows county and state name without tract number.
- Parameters:
df (pd.DataFrame) – DataFrame with GEOID column
- Returns:
DataFrame with NAME column added
- Return type:
pd.DataFrame
- pytidycensus.utils.validate_state(state)[source]
Validate and convert state identifiers to FIPS codes.
- Parameters:
state (str, int, or list) – State name(s), abbreviation(s), or FIPS code(s)
- Returns:
List of 2-digit FIPS codes
- Return type:
List[str]
- Raises:
ValueError – If state identifier is invalid
- pytidycensus.utils.validate_county(county, state_fips)[source]
Validate and convert county identifiers to FIPS codes.
- pytidycensus.utils.lookup_county_fips(county_name, state_fips)[source]
Look up county FIPS code by name.
- pytidycensus.utils.validate_year(year, dataset)[source]
Validate year for given dataset.
- Parameters:
- Returns:
Validated year
- Return type:
- Raises:
ValueError – If year is not available for dataset
- pytidycensus.utils.check_overlapping_acs_periods(years, survey)[source]
Check for and warn about overlapping ACS periods.
Overlapping ACS periods (e.g., 2018 and 2019 for ACS5) share common years and should not be used for statistical comparisons or trend analysis.
- Parameters:
- Return type:
Warning
- UserWarning
If overlapping periods are detected
- pytidycensus.utils.validate_geography(geography, dataset=None)[source]
Validate geography parameter.
- Parameters:
- Returns:
Validated geography
- Return type:
- Raises:
ValueError – If geography is not recognized
NotImplementedError – If geography is recognized but not implemented for the specified dataset
- pytidycensus.utils.build_geography_params(geography, state=None, county=None, **kwargs)[source]
Build geography parameters for Census API call.
- Parameters:
- Returns:
Geography parameters for API call
- Return type:
- Raises:
NotImplementedError – If geography is recognized but not yet implemented
- pytidycensus.utils.process_census_data(data, variables, output='tidy')[source]
Process raw Census API response into pandas DataFrame.