Skip to content

fix(nwis): handle empty peaks response instead of raising KeyError#344

Open
arpitjain099 wants to merge 2 commits into
DOI-USGS:mainfrom
arpitjain099:chore/nwis-empty-peaks
Open

fix(nwis): handle empty peaks response instead of raising KeyError#344
arpitjain099 wants to merge 2 commits into
DOI-USGS:mainfrom
arpitjain099:chore/nwis-empty-peaks

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

Calling nwis.get_discharge_peaks or get_record(service="peaks") for a site with no annual-peak data raises KeyError('peak_dt') instead of returning an empty result.

When peaks has no data, the RDB body is comment lines only, so read_rdb parses it to a column-less empty DataFrame. format_response runs preformat_peaks_response before its own "datetime not in columns" empty-frame check, and that function's first line pops peak_dt, which blows up on the empty frame.

This is the same empty-result behavior that #171 fixed for the other services; peaks slipped through because it gets preformatted first. The fix returns the frame unchanged when peak_dt is absent, so the existing empty-frame path in format_response takes over and callers can check df.empty rather than catching an exception.

Added a regression test in TestReadRdb next to the existing #171 coverage. It fails on main with KeyError('peak_dt') and passes with the change. ruff check and ruff format --check are clean on both touched files.

I work on supply-chain and data-tooling robustness and hit this while looking at the empty-response paths. Happy to adjust if you would rather guard this inside format_response instead.

arpitjain099 and others added 2 commits July 17, 2026 06:03
nwis.get_discharge_peaks / get_record(service="peaks") against a site
with no annual-peak data returns a peaks RDB body of comment lines only,
which read_rdb parses to a column-less empty DataFrame. format_response
runs preformat_peaks_response before its own empty-frame check, and that
function's first statement pops "peak_dt", so the empty case raised
KeyError('peak_dt') instead of returning an empty frame.

This is the same empty-result contract fixed for the other services in
issue DOI-USGS#171; peaks was missed because it is preformatted first. Return the
frame unchanged when peak_dt is absent so the empty-frame path in
format_response handles it and callers can check df.empty.

Adds a regression test alongside the existing DOI-USGS#171 coverage.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
The empty-peaks test parsed with _read_rdb, which already runs
format_response(service=None); the real get_discharge_peaks path uses
the raw read_rdb parser followed by format_response(service="peaks").
Switch to read_rdb so the test exercises the actual call path without
the redundant format pass.

Signed-off-by: thodson-usgs <thodson@usgs.gov>

Co-Authored-By: Claude Opus 4.8 (1M context) <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.

2 participants