Skip to content

Fix KeyError 'GEO_ID' when CensusAPI gets both group and variables - #4

Merged
jinskeep-morpc merged 2 commits into
mainfrom
fix/group-with-variables-fetch
Sep 23, 2026
Merged

jinskeep-morpc merged 2 commits into
mainfrom
fix/group-with-variables-fetch

Conversation

@jinskeep-morpc

Copy link
Copy Markdown
Collaborator

Problem

Passing both group and variables to CensusAPI raised the following in melt(), for ACS and decennial endpoints alike:

KeyError: "The following id_vars or value_vars are not present in the DataFrame: ['GEO_ID']"
CensusAPI(Endpoint('dec/dhc', 2020), group='P1', scope='region15', variables=['P1_001N'])

Root cause

_build_request() sets get=<variables> whenever variables are given, but _fetch() sent any request with a group to _fetch_group(). The API returns GEO_ID/NAME automatically only for the group(...) query form. _fetch_variables() is the path that adds them to get explicitly. With both arguments, a plain variable list went through the group path, so the response had no GEO_ID.

Args get= sent GEO_ID returned
group only group(P1) yes
variables only GEO_ID,NAME,P1_001N yes
both (before fix) P1_001N no

Fix

_fetch() uses _fetch_group() only when variables is None. self.group is unchanged, so checking variables against the group and the group-based concept/universe labels still work.

Changes

  • morpc_census/api.py: dispatch condition in _fetch()
  • tests/test_api.py: new TestFetchDispatch (3 tests); the group+variables test fails before the fix
  • CHANGELOG.md, reference/dev_notes.md: entries
  • pyproject.toml: adds enlighten to dependencies. This is unrelated to the fix and was already pending locally.

Testing

  • Full test suite: 337 passed
  • Live API: group+variables calls on dec/dhc 2020, dec/pl 2020 and acs/acs5 2023 all return long data with geoidfq/name

🤖 Generated with Claude Code

jinskeep-morpc and others added 2 commits September 23, 2026 08:46
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jinskeep-morpc
jinskeep-morpc merged commit f707f5d into main Sep 23, 2026
5 of 7 checks passed
@jinskeep-morpc
jinskeep-morpc deleted the fix/group-with-variables-fetch branch September 23, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant