pytidycensus.get_geography
- pytidycensus.get_geography(geography, year=2022, state=None, county=None, keep_geo_vars=False, cache_dir=None, cb=True, **kwargs)[source]
Download and load geographic boundary data using pygris.
- Parameters:
geography (
str) – Geography type (e.g., ‘county’, ‘tract’, ‘block group’, ‘state’, ‘zcta’, ‘place’)year (
int) – Census year for boundariesstate (
Union[str,int,List[Union[str,int]],None]) – State(s) to filter data for. Can be state name, abbreviation, or FIPS code.county (
Union[str,int,List[Union[str,int]],None]) – County(ies) to filter data for (requires state). Can be county name or FIPS code.keep_geo_vars (
bool) – Whether to keep all geographic variablescache_dir (
Optional[str]) – Directory for caching downloaded files (currently not used with pygris)cb (
bool) – If True, download generalized cartographic boundary files (1:500k). If False, download detailed TIGER/Line files. Note: For 2020 state-level data, cartographic boundaries may fail due to Census Bureau access restrictions. The function will automatically fall back to detailed TIGER/Line files (cb=False) if this occurs.**kwargs – Additional parameters passed to underlying pygris functions
- Returns:
Geographic boundary data
- Return type:
Notes
Automatic Fallback: If downloading cartographic boundary files (cb=True) fails with file system errors (common for 2020 state-level GENZ files), the function will automatically retry with detailed TIGER/Line files (cb=False) and issue a warning. This ensures robust data retrieval without requiring manual intervention.
Examples
>>> # Get county boundaries for Texas >>> tx_counties = get_geography("county", state="TX", year=2022) >>> >>> # Get tract boundaries for Harris County, TX >>> harris_tracts = get_geography( ... "tract", ... state="TX", ... county="201", ... year=2022 ... ) >>> >>> # Get 2020 state boundaries (will auto-fallback if needed) >>> states_2020 = get_geography("state", year=2020)