Reference
Reference data lookups: NAICS codes, SOC codes, state FIPS, sample datasets.
Reference data and crosswalks that ship with the library.
naics_soc_crosswalk— NAICS / SOC code mapping and crosswalkssample_data— built-in demo datasets (synthetic + real Census mash-ups)
Submodules load on first attribute access via PEP 562 __getattr__.
- class siege_utilities.reference.NAICSCode[source]
Bases:
objectA NAICS industry code with hierarchy metadata.
- siege_utilities.reference.parse_naics(code)[source]
Parse a NAICS code string into a
NAICSCode.- Parameters:
code (str) – 2-6 digit NAICS code.
- Return type:
- Raises:
ValueError – If the code is not 2-6 digits.
- siege_utilities.reference.naics_ancestors(code)[source]
Return ancestor codes from sector down to the given code.
>>> naics_ancestors("541511") ['54', '541', '5415', '54151', '541511']
- siege_utilities.reference.naics_to_sector(code)[source]
Return (sector_code, sector_title) for any NAICS code.
- siege_utilities.reference.crosswalk_naics(code, from_year=2017, to_year=2022)[source]
Map a NAICS code from one revision to another.
- class siege_utilities.reference.SOCCode[source]
Bases:
objectA Standard Occupational Classification code.
- siege_utilities.reference.soc_to_major_group(code)[source]
Return (major_code, title) for any SOC code.
- siege_utilities.reference.fuzzy_match_naics(text, candidates=None, threshold=0.5)[source]
Simple token-overlap fuzzy match of text against NAICS sector titles.
- Parameters:
- Returns:
Sorted by score descending.
- Return type:
- siege_utilities.reference.get_naics_lookup()[source]
Return a combined NAICS lookup table (sectors + subsectors).
- siege_utilities.reference.get_soc_lookup()[source]
Return a combined SOC lookup table (major groups + minor groups).
- siege_utilities.reference.naics_title(code)[source]
Look up the title for a NAICS code at any level.
Checks subsector-level first, then falls back to sector. Returns ‘Unknown’ if not found.
- siege_utilities.reference.soc_title(code)[source]
Look up the title for an SOC code at any level.
Checks minor-group level first, then falls back to major group. Returns ‘Unknown’ if not found.
- siege_utilities.reference.filter_by_naics(records, naics_prefix)[source]
Filter NLRB case records by NAICS code prefix.
Works with any iterable of objects that have a
naics_codeattribute (NLRBCaseRecord, NLRBCase model instances, or dicts with ‘naics_code’).- Parameters:
records – Iterable of records to filter.
naics_prefix (str) – NAICS prefix to match (e.g., ‘622’ for hospitals, ‘62’ for all health care).
- Returns:
List of matching records.
- Return type:
Example:
# Show all elections in NAICS 622 (hospitals) hospital_cases = filter_by_naics(result.cases, "622")
- siege_utilities.reference.filter_by_naics_sector(records, sector_code)[source]
Filter records by 2-digit NAICS sector code.
- siege_utilities.reference.group_by_naics_sector(records)[source]
Group records by their 2-digit NAICS sector code.
- siege_utilities.reference.list_available_datasets()[source]
List all available sample datasets with descriptions and metadata.
- siege_utilities.reference.get_dataset_info(dataset_name)[source]
Get detailed information about a specific dataset.
- siege_utilities.reference.load_sample_data(dataset_name, **kwargs)[source]
Load a sample dataset by name.
- Parameters:
dataset_name (str) – Name of the dataset to load
**kwargs – Additional arguments for dataset generation
- Returns:
DataFrame or GeoDataFrame with sample data
- Raises:
ValueError – If dataset name is not recognized
ImportError – If required dependencies are not available
- Return type:
- siege_utilities.reference.get_census_boundaries(year=2020, geographic_level='tract', state_fips=None, county_fips=None)[source]
Download Census geographic boundaries.
- Parameters:
- Returns:
GeoDataFrame with boundaries.
- Raises:
ImportError – If geo extras are not installed.
RuntimeError – If no boundaries are returned from Census source.
- Return type:
- siege_utilities.reference.get_census_data(year=2020, data_type='demographics', geographic_level='tract', state_fips=None, county_fips=None)[source]
Get Census demographic/attribute data.
- Parameters:
- Returns:
DataFrame with Census data or None if failed
- Return type:
pandas.DataFrame | None
- siege_utilities.reference.join_boundaries_and_data(boundaries, data, boundary_id_col='geoid', data_id_col='geoid')[source]
Join geographic boundaries with attribute data.
- Parameters:
boundaries (geopandas.GeoDataFrame) – GeoDataFrame with geographic boundaries
data (pandas.DataFrame) – DataFrame with attribute data
boundary_id_col (str) – Column name for boundary identifiers
data_id_col (str) – Column name for data identifiers
- Returns:
GeoDataFrame with joined data.
- Raises:
KeyError – If the specified ID columns do not exist.
ValueError – If the join produces zero records.
- Return type:
- siege_utilities.reference.create_sample_dataset(year=2020, geographic_level='tract', state_fips='06', county_fips='037', include_geometry=True)[source]
Create a real-world sample dataset by combining boundaries and data.
- Parameters:
- Returns:
Combined dataset.
- Raises:
RuntimeError – If boundaries cannot be retrieved.
NotImplementedError – If Census data retrieval is not yet implemented.
- Return type:
- siege_utilities.reference.get_census_county_sample(state_fips='06', county_fips='037', tract_count=5)[source]
Generate a sample county dataset with multiple tracts and synthetic data.
- Parameters:
- Returns:
DataFrame or GeoDataFrame with county data
- Return type:
- siege_utilities.reference.get_metropolitan_sample(cbsa_code='31080', county_count=3)[source]
Generate a metropolitan area sample with multiple counties.
- Parameters:
- Returns:
DataFrame or GeoDataFrame with metro data
- Return type:
- siege_utilities.reference.generate_synthetic_population(demographics=None, size=1000, geography_level='tract', tract_info=None, include_names=True, include_addresses=True, include_income=True, include_education=True)[source]
Generate synthetic population data matching real demographic patterns.
- Parameters:
demographics (Dict | None) – Dictionary of demographic percentages
size (int) – Number of people to generate
geography_level (str) – Geographic level (tract, county, etc.)
tract_info (Dict | None) – Additional tract information
include_names (bool) – Whether to include synthetic names
include_addresses (bool) – Whether to include synthetic addresses
include_income (bool) – Whether to include synthetic income
include_education (bool) – Whether to include synthetic education
- Returns:
DataFrame with synthetic population data
- Return type:
- siege_utilities.reference.generate_synthetic_businesses(business_count=500, industry_distribution=None, include_locations=True)[source]
Generate synthetic business data with realistic industry patterns.
- Parameters:
- Returns:
DataFrame with synthetic business data
- Return type:
- siege_utilities.reference.generate_synthetic_housing(housing_count=300, property_types=None, include_coordinates=True, locale='us', lat_range=None, lon_range=None, area_unit=None, area_range=None, value_range=None)[source]
Generate synthetic housing data with realistic property patterns.
- Parameters:
housing_count (int) – Number of housing units to generate
property_types (Dict | None) – Dictionary of property type percentages. If None, uses locale-appropriate defaults.
include_coordinates (bool) – Whether to include synthetic coordinates
locale (str) – Country/region preset (‘us’, ‘uk’, ‘de’, ‘fr’, ‘au’). Controls address format, coordinate bounds, units, and value ranges.
lat_range (Tuple[float, float] | None) – Override latitude bounds (min, max)
lon_range (Tuple[float, float] | None) – Override longitude bounds (min, max)
area_unit (str | None) – Override area column name (e.g. ‘square_feet’, ‘square_metres’)
area_range (Tuple[int, int] | None) – Override area range (min, max)
value_range (Tuple[int, int] | None) – Override property value range (min, max)
- Returns:
DataFrame with synthetic housing data
- Return type:
Submodules
NAICS and SOC code crosswalk and normalization.
Maps NLRB industry/occupation codes to Census classification systems:
NAICS (North American Industry Classification System): 2-6 digit hierarchical industry codes with revision crosswalks (2012 → 2017 → 2022).
SOC (Standard Occupational Classification): 2-6 digit occupation codes with revision crosswalks (2010 → 2018).
Used by the cross-tabulation engine (siege_utilities.data.cross_tabulation)
to join NLRB bargaining-unit data with Census industry/occupation tables.
- class siege_utilities.reference.naics_soc_crosswalk.NAICSCode[source]
Bases:
objectA NAICS industry code with hierarchy metadata.
- siege_utilities.reference.naics_soc_crosswalk.parse_naics(code)[source]
Parse a NAICS code string into a
NAICSCode.- Parameters:
code (str) – 2-6 digit NAICS code.
- Return type:
- Raises:
ValueError – If the code is not 2-6 digits.
- siege_utilities.reference.naics_soc_crosswalk.naics_ancestors(code)[source]
Return ancestor codes from sector down to the given code.
>>> naics_ancestors("541511") ['54', '541', '5415', '54151', '541511']
- siege_utilities.reference.naics_soc_crosswalk.naics_to_sector(code)[source]
Return (sector_code, sector_title) for any NAICS code.
- siege_utilities.reference.naics_soc_crosswalk.crosswalk_naics(code, from_year=2017, to_year=2022)[source]
Map a NAICS code from one revision to another.
- class siege_utilities.reference.naics_soc_crosswalk.SOCCode[source]
Bases:
objectA Standard Occupational Classification code.
- siege_utilities.reference.naics_soc_crosswalk.soc_to_major_group(code)[source]
Return (major_code, title) for any SOC code.
- siege_utilities.reference.naics_soc_crosswalk.fuzzy_match_naics(text, candidates=None, threshold=0.5)[source]
Simple token-overlap fuzzy match of text against NAICS sector titles.
- Parameters:
- Returns:
Sorted by score descending.
- Return type:
- siege_utilities.reference.naics_soc_crosswalk.get_naics_lookup()[source]
Return a combined NAICS lookup table (sectors + subsectors).
- siege_utilities.reference.naics_soc_crosswalk.get_soc_lookup()[source]
Return a combined SOC lookup table (major groups + minor groups).
- siege_utilities.reference.naics_soc_crosswalk.naics_title(code)[source]
Look up the title for a NAICS code at any level.
Checks subsector-level first, then falls back to sector. Returns ‘Unknown’ if not found.
- siege_utilities.reference.naics_soc_crosswalk.soc_title(code)[source]
Look up the title for an SOC code at any level.
Checks minor-group level first, then falls back to major group. Returns ‘Unknown’ if not found.
- siege_utilities.reference.naics_soc_crosswalk.filter_by_naics(records, naics_prefix)[source]
Filter NLRB case records by NAICS code prefix.
Works with any iterable of objects that have a
naics_codeattribute (NLRBCaseRecord, NLRBCase model instances, or dicts with ‘naics_code’).- Parameters:
records – Iterable of records to filter.
naics_prefix (str) – NAICS prefix to match (e.g., ‘622’ for hospitals, ‘62’ for all health care).
- Returns:
List of matching records.
- Return type:
Example:
# Show all elections in NAICS 622 (hospitals) hospital_cases = filter_by_naics(result.cases, "622")
- siege_utilities.reference.naics_soc_crosswalk.filter_by_naics_sector(records, sector_code)[source]
Filter records by 2-digit NAICS sector code.
- siege_utilities.reference.naics_soc_crosswalk.group_by_naics_sector(records)[source]
Group records by their 2-digit NAICS sector code.
Sample Data Generation and Management
This module provides built-in sample datasets for testing, learning, and demonstration purposes. Combines real Census data with synthetic personal data using Faker for realistic datasets.
- siege_utilities.reference.sample_data.list_available_datasets()[source]
List all available sample datasets with descriptions and metadata.
- siege_utilities.reference.sample_data.get_dataset_info(dataset_name)[source]
Get detailed information about a specific dataset.
- siege_utilities.reference.sample_data.load_sample_data(dataset_name, **kwargs)[source]
Load a sample dataset by name.
- Parameters:
dataset_name (str) – Name of the dataset to load
**kwargs – Additional arguments for dataset generation
- Returns:
DataFrame or GeoDataFrame with sample data
- Raises:
ValueError – If dataset name is not recognized
ImportError – If required dependencies are not available
- Return type:
- siege_utilities.reference.sample_data.get_census_boundaries(year=2020, geographic_level='tract', state_fips=None, county_fips=None)[source]
Download Census geographic boundaries.
- Parameters:
- Returns:
GeoDataFrame with boundaries.
- Raises:
ImportError – If geo extras are not installed.
RuntimeError – If no boundaries are returned from Census source.
- Return type:
- siege_utilities.reference.sample_data.get_census_data(year=2020, data_type='demographics', geographic_level='tract', state_fips=None, county_fips=None)[source]
Get Census demographic/attribute data.
- Parameters:
- Returns:
DataFrame with Census data or None if failed
- Return type:
pandas.DataFrame | None
- siege_utilities.reference.sample_data.join_boundaries_and_data(boundaries, data, boundary_id_col='geoid', data_id_col='geoid')[source]
Join geographic boundaries with attribute data.
- Parameters:
boundaries (geopandas.GeoDataFrame) – GeoDataFrame with geographic boundaries
data (pandas.DataFrame) – DataFrame with attribute data
boundary_id_col (str) – Column name for boundary identifiers
data_id_col (str) – Column name for data identifiers
- Returns:
GeoDataFrame with joined data.
- Raises:
KeyError – If the specified ID columns do not exist.
ValueError – If the join produces zero records.
- Return type:
- siege_utilities.reference.sample_data.create_sample_dataset(year=2020, geographic_level='tract', state_fips='06', county_fips='037', include_geometry=True)[source]
Create a real-world sample dataset by combining boundaries and data.
- Parameters:
- Returns:
Combined dataset.
- Raises:
RuntimeError – If boundaries cannot be retrieved.
NotImplementedError – If Census data retrieval is not yet implemented.
- Return type:
- siege_utilities.reference.sample_data.get_census_county_sample(state_fips='06', county_fips='037', tract_count=5)[source]
Generate a sample county dataset with multiple tracts and synthetic data.
- Parameters:
- Returns:
DataFrame or GeoDataFrame with county data
- Return type:
- siege_utilities.reference.sample_data.get_metropolitan_sample(cbsa_code='31080', county_count=3)[source]
Generate a metropolitan area sample with multiple counties.
- Parameters:
- Returns:
DataFrame or GeoDataFrame with metro data
- Return type:
- siege_utilities.reference.sample_data.generate_synthetic_population(demographics=None, size=1000, geography_level='tract', tract_info=None, include_names=True, include_addresses=True, include_income=True, include_education=True)[source]
Generate synthetic population data matching real demographic patterns.
- Parameters:
demographics (Dict | None) – Dictionary of demographic percentages
size (int) – Number of people to generate
geography_level (str) – Geographic level (tract, county, etc.)
tract_info (Dict | None) – Additional tract information
include_names (bool) – Whether to include synthetic names
include_addresses (bool) – Whether to include synthetic addresses
include_income (bool) – Whether to include synthetic income
include_education (bool) – Whether to include synthetic education
- Returns:
DataFrame with synthetic population data
- Return type:
- siege_utilities.reference.sample_data.generate_synthetic_businesses(business_count=500, industry_distribution=None, include_locations=True)[source]
Generate synthetic business data with realistic industry patterns.
- Parameters:
- Returns:
DataFrame with synthetic business data
- Return type:
- siege_utilities.reference.sample_data.generate_synthetic_housing(housing_count=300, property_types=None, include_coordinates=True, locale='us', lat_range=None, lon_range=None, area_unit=None, area_range=None, value_range=None)[source]
Generate synthetic housing data with realistic property patterns.
- Parameters:
housing_count (int) – Number of housing units to generate
property_types (Dict | None) – Dictionary of property type percentages. If None, uses locale-appropriate defaults.
include_coordinates (bool) – Whether to include synthetic coordinates
locale (str) – Country/region preset (‘us’, ‘uk’, ‘de’, ‘fr’, ‘au’). Controls address format, coordinate bounds, units, and value ranges.
lat_range (Tuple[float, float] | None) – Override latitude bounds (min, max)
lon_range (Tuple[float, float] | None) – Override longitude bounds (min, max)
area_unit (str | None) – Override area column name (e.g. ‘square_feet’, ‘square_metres’)
area_range (Tuple[int, int] | None) – Override area range (min, max)
value_range (Tuple[int, int] | None) – Override property value range (min, max)
- Returns:
DataFrame with synthetic housing data
- Return type: