cmsetzer / autocensus

Python package for collecting ACS and geospatial data from the Census API
MIT License
21 stars 6 forks source link

TypeError: str argument expected #24

Closed ghost closed 2 months ago

ghost commented 8 months ago

Hi again! I was running a query that worked before without any issues. I haven't changed anything, but I'm receiving this error. I think it has to do with the geography variables - I'm trying to pull all states and all counties. When I specific one state and one county, the query works. Any suggestions on why this might not work now? Thank you!


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 4
      1 #@title ACS 1-Year Estimates Detailed Tables - B01001B | Sex by Age (Black or African American Alone)
      2 
      3 # Configure query
----> 4 query3 = Query(
      5     estimate=1,
      6     years=[2022],
      7     variables=['B01001B_001E',  'B01001B_001EA',    'B01001B_001M', 'B01001B_001MA',    'B01001B_002E', 'B01001B_002EA',    'B01001B_002M', 'B01001B_002MA',    'B01001B_003E', 'B01001B_003EA',    'B01001B_003M', 'B01001B_003MA',    'B01001B_004E', 'B01001B_004EA',    'B01001B_004M', 'B01001B_004MA',    'B01001B_005E', 'B01001B_005EA',    'B01001B_005M', 'B01001B_005MA',    'B01001B_006E', 'B01001B_006EA',    'B01001B_006M', 'B01001B_006MA',    'B01001B_007E', 'B01001B_007M', 'B01001B_007MA',    'B01001B_007EA',    'B01001B_008E', 'B01001B_008EA',    'B01001B_008M', 'B01001B_008MA',    'B01001B_009E', 'B01001B_009EA',    'B01001B_009M', 'B01001B_009MA',    'B01001B_010E', 'B01001B_010EA',    'B01001B_010M', 'B01001B_010MA',    'B01001B_011E', 'B01001B_011EA',    'B01001B_011M', 'B01001B_011MA',    'B01001B_012E', 'B01001B_012EA',    'B01001B_012M', 'B01001B_012MA',    'B01001B_013E', 'B01001B_013EA',    'B01001B_013M', 'B01001B_013MA',    'B01001B_014E', 'B01001B_014EA',    'B01001B_014M', 'B01001B_014MA',    'B01001B_015E', 'B01001B_015EA',    'B01001B_015M', 'B01001B_015MA',    'B01001B_016E', 'B01001B_016EA',    'B01001B_016M', 'B01001B_016MA',    'B01001B_017E', 'B01001B_017EA',    'B01001B_017M', 'B01001B_017MA',    'B01001B_018E', 'B01001B_018EA',    'B01001B_018M', 'B01001B_018MA',    'B01001B_019E', 'B01001B_019EA',    'B01001B_019M', 'B01001B_019MA',    'B01001B_020E', 'B01001B_020EA',    'B01001B_020M', 'B01001B_020MA',    'B01001B_021E', 'B01001B_021EA',    'B01001B_021M', 'B01001B_021MA',    'B01001B_022E', 'B01001B_022EA',    'B01001B_022M', 'B01001B_022MA',    'B01001B_023E', 'B01001B_023M', 'B01001B_023MA',    'B01001B_023EA',    'B01001B_024E', 'B01001B_024M', 'B01001B_024MA',    'B01001B_024EA',    'B01001B_025E', 'B01001B_025M', 'B01001B_025MA',    'B01001B_025EA',    'B01001B_026E', 'B01001B_026EA',    'B01001B_026M', 'B01001B_026MA',    'B01001B_027E', 'B01001B_027EA',    'B01001B_027M', 'B01001B_027MA',    'B01001B_028E', 'B01001B_028M', 'B01001B_028MA',    'B01001B_028EA',    'B01001B_029E', 'B01001B_029M', 'B01001B_029MA',    'B01001B_029EA',    'B01001B_030E', 'B01001B_030EA',    'B01001B_030M', 'B01001B_030MA',    'B01001B_031E', 'B01001B_031EA',    'B01001B_031M', 'B01001B_031MA',
      8 ],
      9     for_geo='county:*',
     10     in_geo=['state:*'],
     11     # Optional arg to add geometry: 'points', 'polygons', or None (default)
     12     geometry='points',
     13     # Fill in the following with your actual Census API key
     14     census_api_key=key
     15 )
     17 # Run query and collect output in dataframe
     18 acs_oneyr_B01001B = query3.run()

File ~/.local/lib/python3.10/site-packages/autocensus/query.py:90, in Query.__init__(self, estimate, years, variables, for_geo, in_geo, geometry, resolution, census_api_key)
     87 self._variables = wrap_scalar_value_in_list(variables)
     88 self.for_geo = [Geo(geo) for geo in wrap_scalar_value_in_list(for_geo)]
     89 self.in_geo = (
---> 90     [] if in_geo is None else [Geo(geo) for geo in wrap_scalar_value_in_list(in_geo)]
     91 )
     93 # Validate geometry and resolution
     94 if geometry is None or geometry in GEOMETRIES:

File ~/.local/lib/python3.10/site-packages/autocensus/query.py:90, in <listcomp>(.0)
     87 self._variables = wrap_scalar_value_in_list(variables)
     88 self.for_geo = [Geo(geo) for geo in wrap_scalar_value_in_list(for_geo)]
     89 self.in_geo = (
---> 90     [] if in_geo is None else [Geo(geo) for geo in wrap_scalar_value_in_list(in_geo)]
     91 )
     93 # Validate geometry and resolution
     94 if geometry is None or geometry in GEOMETRIES:

File ~/.local/lib/python3.10/site-packages/autocensus/geography.py:48, in Geo.__init__(self, value, code)
     46 # Convert state abbreviation to FIPS code as needed
     47 if (self.type == 'state') and (code != '*'):
---> 48     state = us.states.lookup(self.code)
     49     if state is not None:
     50         self.code = state.fips

File ~/.local/lib/python3.10/site-packages/us/states.py:86, in lookup(val, field, use_cache)
     84         field = "abbr"
     85     else:
---> 86         val = jellyfish.metaphone(val)
     87         field = "name_metaphone"
     89 # see if result is in cache

TypeError: str argument expected
cmsetzer commented 8 months ago

Hi again @datarebellion, I've tried the query you shared but have not been able to reproduce this error. Since you mention that the same query was working before, I wonder if there was a weird transient issue with the Census API response (this is not unheard of).

If you are able to reproduce the error again, it would be useful to know the following: