Skip to content

Fix IndexError in _read_rdb when NWIS returns no data rows#227

Merged
thodson-usgs merged 2 commits intoDOI-USGS:mainfrom
thodson-usgs:fix-get-stats-no-sites
Apr 13, 2026
Merged

Fix IndexError in _read_rdb when NWIS returns no data rows#227
thodson-usgs merged 2 commits intoDOI-USGS:mainfrom
thodson-usgs:fix-get-stats-no-sites

Conversation

@thodson-usgs
Copy link
Copy Markdown
Collaborator

@thodson-usgs thodson-usgs commented Apr 13, 2026

Summary

Fixes #171.

When the NWIS stats service has no data for a site, it returns a valid HTTP 200 with only comment lines:

# //Output-Format: RDB
# //Response-Status: OK
# //Response-Message: No sites found matching all criteria

_read_rdb tried to index lines[count] for field names after counting comment lines, but with no data rows count == len(lines), causing an unhelpful IndexError.

Since this is a legitimate empty result (not an error), the fix returns an empty DataFrame so callers can use the idiomatic df.empty check:

df, md = nwis.get_stats(sites="02469675")
if df.empty:
    print("No data for this site")

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 case
  • TestReadRdb::test_valid_rdb_returns_dataframe — regression: normal responses still parse correctly
  • Full tests/nwis_test.py suite passes (30/30)

🤖 Generated with Claude Code

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>
@thodson-usgs thodson-usgs force-pushed the fix-get-stats-no-sites branch from 3358dd0 to 7c9e354 Compare April 13, 2026 22:00
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>
@thodson-usgs thodson-usgs force-pushed the fix-get-stats-no-sites branch from 3fa5d28 to 0ddb05c Compare April 13, 2026 22:18
@thodson-usgs thodson-usgs marked this pull request as ready for review April 13, 2026 22:26
@thodson-usgs thodson-usgs merged commit 0dfaec4 into DOI-USGS:main Apr 13, 2026
8 checks passed
@thodson-usgs thodson-usgs deleted the fix-get-stats-no-sites branch April 13, 2026 22:28
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.

Create better error handling with nwis.get_stats

1 participant