Make _handle_stats_nesting tolerant of missing drop columns#257
Merged
thodson-usgs merged 1 commit intoDOI-USGS:mainfrom May 4, 2026
Merged
Conversation
Both `.drop()` calls in `_handle_stats_nesting` (for the geopandas and pandas branches) hardcoded literal column names — `["type", "properties.data"]` and `["data"]`. If a stats response is ever returned in a slightly different shape (or one of those keys is renamed/removed), `drop()` raises `KeyError` and aborts the helper. The sibling `pd.json_normalize(...)` call later in the same function already passes `errors="ignore"`, so add the same to the two `drop()` calls for parity. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 4, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the dataretrieval.waterdata.utils._handle_stats_nesting helper against minor upstream schema variations by making its initial DataFrame/GeoDataFrame column drops tolerant of missing columns, preventing avoidable KeyErrors during stats response normalization.
Changes:
- Add
errors="ignore"to the pandas-branchdrop(columns=["type", "properties.data"])call. - Add
errors="ignore"to the geopandas-branchdrop(columns=["data"])call. - Add a regression test ensuring
_handle_stats_nestingsucceeds when a drop-target column (e.g.,type) is absent.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
dataretrieval/waterdata/utils.py |
Makes _handle_stats_nesting resilient to missing columns during initial flattening by ignoring absent drop targets. |
tests/waterdata_utils_test.py |
Adds regression coverage for missing drop-target columns in stats feature normalization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_handle_stats_nestinghas two.drop(columns=...)calls (one for the geopandas branch, one for the pandas branch) that hardcode literal column names —[\"type\", \"properties.data\"]and[\"data\"]respectively. If a stats response ever comes back in a slightly different shape (renamed key, missing optional key, edge-case feature without the expected nesting),drop()raisesKeyErrorand aborts the helper.The sibling
pd.json_normalize(...)call later in the same function already passeserrors=\"ignore\", so this PR adds the same to the twodrop()calls for parity.Diff
Test plan
test_handle_stats_nesting_tolerates_missing_drop_columnsconstructs a stats body whose features lack the top-leveltypekey and confirms the function returns a populated DataFrame.mainwithKeyError: \"['type'] not found in axis\".nwis_test.py): 197 passed.🤖 Generated with Claude Code
Related PRs
Other open PRs in this bug-review series that touch
dataretrieval/waterdata/utils.py(different functions, no functional conflicts):_format_api_datesaccept ISO 8601._arrange_colsstop mutating caller list._handle_stats_nestingtolerate missing drop columns.