walkerke / pygris

Use US Census shapefiles in Python (port of the R tigris package)
https://walker-data.com/pygris
MIT License
107 stars 16 forks source link

Error with user-specified params in data.get_census() #13

Closed jvtcl closed 11 months ago

jvtcl commented 11 months ago

I'm running into some trouble executing an ACS request with data.get_census() and user-specified params:

get_census(
    dataset="acs/acs5",
    variables=['B01001_001E', 'B09019_026E', 'B27010_001E', 'B25001_001E', 'B25003_001E'],
    params={'for': 'block+group:*', 'in': 'state:47+county:093+tract:*'},
    year=2019,
)

This results in a failed request:

Traceback (most recent call last):

  File ~/miniconda3/envs/py311_livelike/lib/python3.11/site-packages/IPython/core/interactiveshell.py:3548 in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  Cell In[35], line 1
    get_census(

  File ~/projects/pygris/pygris/data.py:85 in get_census
    raise SyntaxError(f"Request failed. The Census Bureau error message is {req.text}")

  File <string>
SyntaxError: Request failed. The Census Bureau error message is error: invalid 'for' argument

However, I can generate a URL containing the same variables and parameters, and the request is successful: https://api.census.gov/data/2019/acs/acs5?get=B01001_001E,B09019_026E,B27010_001E,B25001_001E,B25003_001E&for=block+group:*&in=state:47+county:093+tract:*

jvtcl commented 11 months ago

To follow on, it seems the request failure is tied to generating queries for block groups.

This call successfully returns results (also when using a wildcard for tract ID):

get_census(
    dataset="acs/acs5",
    variables=['B01001_001E', 'B09019_026E', 'B27010_001E', 'B25001_001E', 'B25003_001E'],
    params={'for': 'tract:003700', 'in': 'state:47+county:093'},
    year=2019,
    return_geoid=True,
)

But this one fails with the same error as above:

get_census(
    dataset="acs/acs5",
    variables=['B01001_001E', 'B09019_026E', 'B27010_001E', 'B25001_001E', 'B25003_001E'],
    params={'for': 'block+group:1', 'in': 'state:47+county:093+tract:003700'},
    year=2019,
)
jvtcl commented 11 months ago

Seems I got this sorted: block group should be written with a space and not a + separator. The separator is added under the hood here.

The following now works:

get_census(
    dataset="acs/acs5",
    variables=['B01001_001E', 'B09019_026E', 'B27010_001E', 'B25001_001E', 'B25003_001E'],
    params={'for': 'block group:1', 'in': 'state:47+county:093+tract:003700'},
    year=2019,
)