Skip to content

Fix countyLookup, which raised for every scope - #172

Open
jinskeep-morpc wants to merge 1 commit into
mainfrom
fix/countylookup-pandas2
Open

Fix countyLookup, which raised for every scope#172
jinskeep-morpc wants to merge 1 commit into
mainfrom
fix/countylookup-pandas2

Conversation

@jinskeep-morpc

Copy link
Copy Markdown
Collaborator

morpc.countyLookup could not be instantiated. Two bugs, either of which alone made the class unusable:

>>> morpc.countyLookup(scope="oh")
ValueError: cannot use columns parameter with orient='columns'

__init__ called DataFrame.from_dict with a columns argument. pandas accepts columns only for orient="index" or "tight" and rejects it for the default orient, so every scope raised. CONST_COUNTY_NAME_TO_ID maps name → GEOID, so the two columns come from its .items().

get_id indexed on a COUNTY_NAME column. The dataframe is filtered to GEOID and NAME a few lines earlier, so that column does not exist — get_id would have raised KeyError even if the constructor had worked. This one is invisible until the first bug is fixed, which is presumably why it survived.

Both are longstanding rather than a recent regression.

Why it went unnoticed

The class had no test coverage. Downstream, repos worked around it rather than reporting it — morpc-odrcfacilities-standardize reads CONST_COUNTY_NAME_TO_ID directly with the comment "the class is currently broken against pandas 2.x". morpc-repotemplate-standardize recommended the broken call in its transform section, so every repo scaffolded from the template inherited a step that fails on a current environment. I have a companion PR removing that recommendation from the template (morpc/morpc-repotemplate-standardize#1); it can stay as-is once this lands, since CONST_COUNTY_NAME_TO_ID is the same mapping either way.

Tests

10 new tests in tests/test_morpc.py, covering the scope aliases (oh / ohio / morpc / 15-County Region / REGION15 / any CONST_REGIONS key), the name ↔ GEOID round trip, sort order and name/id alignment, and the three ways a lookup should raise: unknown scope, unknown county, and a county outside the requested scope.

All 10 fail on main and pass on this branch.

scope="us" is deliberately uncovered — it queries the Census API, and get_id is unsupported for it anyway.

Test run

144 passed, 4 failed. The 4 failures are all in tests/test_utils.py (ISO 8601 / tzinfo handling in datetime_from_string), fail identically on main, and are unrelated to this change.

🤖 Generated with Claude Code

Two bugs, either of which made the class unusable:

  - The constructor called DataFrame.from_dict with a columns argument.
    pandas accepts columns only for orient="index" or "tight" and rejects it
    for the default orient, so instantiating countyLookup raised ValueError
    regardless of scope. CONST_COUNTY_NAME_TO_ID maps name -> GEOID, so the
    two columns come from its items.

  - get_id indexed on a COUNTY_NAME column. The dataframe is filtered to
    GEOID and NAME a few lines earlier, so the column does not exist and the
    call would have raised KeyError even had the constructor worked.

Both are longstanding. The class had no test coverage, so nothing caught
them; downstream repos worked around it by reading CONST_COUNTY_NAME_TO_ID
directly, and morpc-repotemplate-standardize shipped the broken call to every
repo scaffolded from it.

Add tests covering the scope aliases, the name/GEOID round trip, sort order,
and the three ways a lookup should raise. scope="us" is left uncovered: it
queries the Census API, and get_id is unsupported for it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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