Package: autocensus
autocensusA Python package for collecting American Community Survey (ACS) data and associated geometry from theCensus APIin apandasdataframe.ContentsInstallationQuickstartGeometryPointsPolygonsShapefile resolutionShapefile cachingInstallationautocensus requires Python 3.8 or higher. Install as follows:pipinstallautocensusTo run autocensus, you must specify aCensus API keyvia either thecensus_api_keykeyword argument (as shown in the example below) or by setting the environment variableCENSUS_API_KEY.QuickstartfromautocensusimportQuery# Configure queryquery=Query(estimate=1,years=[2017,2018],variables=['DP03_0025E','S0103_C01_104E'],for_geo='county:033',in_geo=['state:53'],# Optional arg to add geometry: 'points', 'polygons', or None (default)geometry='points',# Fill in the following with your actual Census API keycensus_api_key='Your Census API key')# Run query and collect output in dataframedataframe=query.run()Output:namegeo_idgeo_typeyeardatevariable_codevariable_labelvariable_conceptannotationvaluegeometryKing County, Washington0500000US53033county20172017-12-31DP03_0025EEstimate!!COMMUTING TO WORK!!Mean travel time to work (minutes)SELECTED ECONOMIC CHARACTERISTICS30.0POINT (…)King County, Washington0500000US53033county20182018-12-31DP03_0025EEstimate!!COMMUTING TO WORK!!Workers 16 years and over!!Mean travel time to work (minutes)SELECTED ECONOMIC CHARACTERISTICS30.2POINT (…)King County, Washington0500000US53033county20172017-12-31S0103_C01_104ETotal!!Estimate!!GROSS RENT!!Median gross rent (dollars)POPULATION 65 YEARS AND OVER IN THE UNITED STATES1555.0POINT (…)King County, Washington0500000US53033county20182018-12-31S0103_C01_104EEstimate!!Total!!Renter-occupied housing units!!GROSS RENT!!Median gross rent (dollars)POPULATION 65 YEARS AND OVER IN THE UNITED STATES1674.0POINT (…)Geometryautocensus supports point- and polygon-based geometry data for many years and geographies by way of the Census Bureau'sGazetteer FilesandCartographic Boundary Files.Here's how to add geometry to your data:PointsPoint data from the Census Bureau's Gazetteer Files is generally available for years from 2012 on in the following geographies:Nation-levelurban areazip code tabulation areacountycongressional districtmetropolitan statistical area/micropolitan statistical areaamerican indian area/alaska native area/hawaiian home landState-levelcounty subdivisiontractplacestate legislative district (upper chamber)state legislative district (lower chamber)Example:fromautocensusimportQueryquery=Query(estimate=5,years=[2018],variables=['DP03_0025E'],for_geo=['county:033'],in_geo=['state:53'],geometry='points')dataframe=query.run()PolygonsPolygon data from the Census Bureau's Cartographic Boundary Shapefiles is generally available for years from 2013 on in the following geographies:Nation-levelnationregiondivisionstateurban areazip code tabulation areacountycongressional districtmetropolitan statistical area/micropolitan statistical areacombined statistical areaamerican indian area/alaska native area/hawaiian home landnew england city and town areaState-levelalaska native regional corporationblock groupcounty subdivisiontractplacepublic use microdata areastate legislative district (upper chamber)state legislative district (lower chamber)Example:fromautocensusimportQueryquery=Query(estimate=5,years=[2018],variables=['DP03_0025E'],for_geo=['county:033'],in_geo=['state:53'],geometry='polygons')dataframe=query.run()Shapefile resolutionBy default, autocensus will attempt to fetch almost all shapefiles at a resolution of 1 : 500,000 (500k). Some sources among the Cartographic Boundary Shapefiles are also available at the lower resolutions of 1 : 5,000,000 (5m) or 1 : 20,000,000 (20m). To attempt to download a shapefile at a specific resolution, pass a value toQuery's optionalresolutionparameter:fromautocensusimportQueryquery=Query(estimate=5,years=[2018],variables=['DP03_0025E'],for_geo=['county:*'],in_geo=['state:53'],geometry='polygons',# Optional arg to set a specific resolution: '500k', '5m', or '20m'resolution='20m')Setting a specific resolution is only supported for polygon-based geometry.Shapefile cachingTo improve performance across queries that include polygon-based geometry data, autocensus will cache Census shapefiles on disk by default. The cache directory location depends on your OS; you can look it up fromautocensus.constants.CACHE_DIRECTORY_PATHlike so:python-c"import autocensus; print(autocensus.constants.CACHE_DIRECTORY_PATH)"Sometimes it is useful to clear this cache directory, especially if you're running into persistent shapefile-related problems. You can clear the cache by manually deleting the cache directory or by executing theautocensus.clear_cachefunction:python-c"import autocensus; autocensus.clear_cache()"