By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just noticed this issue starting today. Doing a basic ACS5 query is now raising a KeyError.

import os
from census import Census
census = Census(os.environ['CENSUS_API_KEY'])
census.acs5.get(
    ['B01001_001E'],
    {'for': 'state:04'},
    year=2020,

Exception:

KeyError                                  Traceback (most recent call last)
Cell In[1], line 6
      3 from census import Census
      5 census = Census(os.environ['CENSUS_API_KEY'])
----> 6 census.acs5.get(
      7     ['B01001_001E'],
      8     {'for': 'state:04'},
      9     year=2020,
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:318, in ACSClient.get(self, *args, **kwargs)
    315 def get(self, *args, **kwargs):
    316     self._switch_endpoints(kwargs.get('year', self.default_year))
--> 318     return super(ACSClient, self).get(*args, **kwargs)
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:159, in Client.get(self, fields, geo, year, **kwargs)
    156 sort_by_geoid = len(fields) > 49 and (not year or year > 2009)
    157 all_results = (self.query(forty_nine_fields, geo, year, sort_by_geoid=sort_by_geoid, **kwargs)
    158                for forty_nine_fields in chunks(fields, 49))
--> 159 merged_results = [merge(result) for result in zip(*all_results)]
    161 return merged_results
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:157, in <genexpr>(.0)
    150 \"\"\"
    151 The API only accepts up to 50 fields on each query.
    152 Chunk requests, and use the unique GEO_ID to match up the chunks
    153 in case the responses are in different orders.
    154 GEO_ID is not reliably present in pre-2010 requests.
    155 \"\"\"
    156 sort_by_geoid = len(fields) > 49 and (not year or year > 2009)
--> 157 all_results = (self.query(forty_nine_fields, geo, year, sort_by_geoid=sort_by_geoid, **kwargs)
    158                for forty_nine_fields in chunks(fields, 49))
    159 merged_results = [merge(result) for result in zip(*all_results)]
    161 return merged_results
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:60, in retry_on_transient_error.<locals>.wrapper(self, *args, **kwargs)
     58 for _ in range(max(self.retries - 1, 0)):
     59     try:
---> 60         result = func(self, *args, **kwargs)
     61     except CensusException as e:
     62         if \"There was an error while running your query.  We've logged the error and we'll correct it ASAP.  Sorry for the inconvenience.\" in str(e):
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:198, in Client.query(self, fields, geo, year, sort_by_geoid, **kwargs)
    195         raise ex
    197 headers = data.pop(0)
--> 198 types = [self._field_type(header, year) for header in headers]
    199 results = [{header: (cast(item) if item is not None else None)
    200             for header, cast, item
    201             in zip(headers, types, d)}
    202            for d in data]
    203 if sort_by_geoid:
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:198, in <listcomp>(.0)
    195         raise ex
    197 headers = data.pop(0)
--> 198 types = [self._field_type(header, year) for header in headers]
    199 results = [{header: (cast(item) if item is not None else None)
    200             for header, cast, item
    201             in zip(headers, types, d)}
    202            for d in data]
    203 if sort_by_geoid:
File ~/Workspaces/Epymorph/.venv/lib/python3.11/site-packages/census/core.py:230, in Client._field_type(self, field, year)
    228 if resp.status_code == 200:
    229     predicate_type = resp.json().get(\"predicateType\", \"string\")
--> 230     return types[predicate_type]
    231 else:
    232     return str
KeyError: 'long'"

It appears as though the Census variables resource now reports these types as "long" which isn't in the supported types mapping.

I installed census yesterday and am still having this issue. I have tested with ACS 5 year variables B25064_001E and B25070_001E, ACS1 B25064_001E, ACS5DP DP04_0132E, and ACS1DP DP04_0132E. I have the same problem with SF1 variables.

The package overall has been great and I appreciate the work, but unfortunately right now it is not usable.

Could somebody please help fix this issue or suggest a workaround?
I have the same KeyError: 'long' and I need to do a query for median income.
Thanks a lot.

Could somebody please help fix this issue or suggest a workaround? I have the same KeyError: 'long' and I need to do a query for median income. Thanks a lot.

A fix has been pushed out - If you're using pip, you should be able to install it by doing
pip install --upgrade git+git://github.com/datamade/census@master