Fix IndexError in _read_rdb when NWIS returns no data rows#227
Merged
thodson-usgs merged 2 commits intoDOI-USGS:mainfrom Apr 13, 2026
Merged
Fix IndexError in _read_rdb when NWIS returns no data rows#227thodson-usgs merged 2 commits intoDOI-USGS:mainfrom
thodson-usgs merged 2 commits intoDOI-USGS:mainfrom
Conversation
When an NWIS service responds with only comment lines (e.g. "No sites found matching all criteria"), _read_rdb tried to index past the end of the lines list and raised an unhelpful IndexError. Now it detects the empty-data case, extracts the Response-Message from the comment block, and raises a ValueError with that message instead. Fixes DOI-USGS#171. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3358dd0 to
7c9e354
Compare
When the NWIS service responds with only comment lines (e.g. "No sites found matching all criteria"), _read_rdb indexed past the end of the line list and raised an unhelpful IndexError. Since this is a legitimate empty result rather than an error, the fix returns an empty DataFrame so callers can use the idiomatic df.empty check instead of catching an exception. Fixes DOI-USGS#171. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3fa5d28 to
0ddb05c
Compare
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
Fixes #171.
When the NWIS stats service has no data for a site, it returns a valid HTTP 200 with only comment lines:
_read_rdbtried to indexlines[count]for field names after counting comment lines, but with no data rowscount == len(lines), causing an unhelpfulIndexError.Since this is a legitimate empty result (not an error), the fix returns an empty
DataFrameso callers can use the idiomaticdf.emptycheck:Test plan
TestReadRdb::test_no_sites_returns_empty_dataframe— uses the exact response body from the live endpoint (https://waterservices.usgs.gov/nwis/stat/?sites=02469675)TestReadRdb::test_all_comments_returns_empty_dataframe— generic all-comments caseTestReadRdb::test_valid_rdb_returns_dataframe— regression: normal responses still parse correctlytests/nwis_test.pysuite passes (30/30)🤖 Generated with Claude Code