I am working on a project where I am trying to analyze a lot of ACS census data from Pittsburgh, PA, USA. I can easily go to data.census.gov to grab the data I need for the 138 census tracts I am looking for, but that is simply not efficient. So I downloaded the cenpy library which has been working great for New York City ACS data. Here is an example for New York City:
NYC_income = products.ACS(2019).from_place('New York City, NY', level = 'tract', variables = ['B19013_001E'])
This works fine and will give me a geodataframe with the ACS variables I pass in. I have tried this for Pittsburgh and it does not work and the errors are not very helpful:
pgh_Test = products.ACS(2019).from_place('Pittsburgh, PA', level='tract', variables = ['B01001A_001E'])
This will return an error:
KeyError: 'Response from API is malformed. You may have submitted too many queries, formatted the request incorrectly, or experienced significant network connectivity issues. Check to make sure that your inputs, like placenames, are spelled correctly, and that your geographies match the level at which you intend to query. The original error from the Census is:\n(API ERROR 400:Failed to execute query.([]))'
I have also tried other varients of how to spell Pittsburgh
such as Pittsburgh City
, Pittsburgh city
, Pittsburg
, and also tried spelling out the state instead of using acronym.
Ultimately, I am curious if anyone has run into this issue and how to fix it so that I can access Pittsburgh ACS data via cenpy instead of selecting every individual census tract through data.census.gov.
Thank you in advance!
Advertisement
Answer
Use 'County Subdivision'
as place_type
. It seems that it helps to resolve the place correctly:
products.ACS(2019).from_place('Pittsburgh, PA', place_type='County Subdivision', level='tract', variables = ['B01001A_001E'])
Output:
Matched: Pittsburgh, PA to Pittsburgh city within layer County Subdivisions GEOID geometry B01001A_001E state county tract 0 42003270300 POLYGON ((-8910344.550 4935795.800, -8910341.7... 1154.0 42 003 270300 1 42003980600 POLYGON ((-8909715.600 4933176.800, -8909606.6... 13.0 42 003 980600 2 42003051100 POLYGON ((-8903296.360 4930484.040, -8903251.9... 0.0 42 003 051100 3 42003050900 POLYGON ((-8903766.910 4931335.660, -8903642.5... 40.0 42 003 050900 4 42003562000 POLYGON ((-8901104.700 4930705.200, -8901104.1... 1826.0 42 003 562000 ... ... ... ... ... ... ... 84 42003980500 POLYGON ((-8899981.160 4929217.570, -8899977.7... 16.0 42 003 980500 85 42003140200 POLYGON ((-8898569.740 4931230.040, -8898532.8... 1932.0 42 003 140200 86 42003111300 POLYGON ((-8898077.150 4934571.530, -8898053.1... 1499.0 42 003 111300 87 42003111500 POLYGON ((-8898240.670 4932660.630, -8898229.9... 942.0 42 003 111500 88 42003120700 POLYGON ((-8895502.550 4932516.230, -8895493.0... 17.0 42 003 120700 89 rows × 6 columns
Other values for this argument are 'Incorporated Place'
and 'Census Designated Place'
. From the documentation:
place_type : str
type of place to focus on, Incorporated Place, County Subdivision, or Census Designated Place.
See a demo in this colab.