Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nwis_client bug causing KeyError when station does not return data #134

Merged
merged 7 commits into from
Sep 9, 2021
2 changes: 1 addition & 1 deletion python/nwis_client/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = hydrotools.nwis_client
version = 3.0.4
version = 3.0.5
author = Austin Raney
author_email = [email protected]
description = A convenient interface to the USGS NWIS Instantaneous Values (IV) REST Service API.
Expand Down
28 changes: 25 additions & 3 deletions python/nwis_client/src/hydrotools/nwis_client/iv.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,25 @@ def list_to_df_helper(item: dict):

return df

def empty_df_warning_helper():
warning_message = "No data was returned by the request."
warnings.warn(warning_message)

list_of_frames = list(map(list_to_df_helper, raw_data))

# Empty list. No data was returned in the request
if not list_of_frames:
warning_message = "No data was returned by the request."
warnings.warn(warning_message)
return pd.DataFrame(None)
empty_df_warning_helper()
return _create_empty_canonical_df()

# Concatenate list in single pd.DataFrame
dfs = pd.concat(list_of_frames, ignore_index=True)

# skip data processing steps if no data was retrieved and return empty canonical df
if dfs.empty:
empty_df_warning_helper()
return _create_empty_canonical_df()

# Convert values to numbers
dfs.loc[:, "value"] = pd.to_numeric(dfs["value"], downcast="float")

Expand Down Expand Up @@ -809,3 +817,17 @@ def flatten_and_stringify(v):
value_groups = np.array_split(values, n_groups)

return list(map(lambda i: ",".join(i), value_groups))


def _create_empty_canonical_df() -> pd.DataFrame:
"""Returns an empty hydrotools canonical dataframe with correct field datatypes."""
cols = {
"value_time": pd.Series(dtype="datetime64[ns]"),
aaraney marked this conversation as resolved.
Show resolved Hide resolved
"variable_name": pd.Series(dtype="category"),
"usgs_site_code": pd.Series(dtype="category"),
"measurement_unit": pd.Series(dtype="category"),
"value": pd.Series(dtype="float32"),
"qualifiers": pd.Series(dtype="category"),
"series": pd.Series(dtype="category"),
}
return pd.DataFrame(cols, index=[])
14 changes: 14 additions & 0 deletions python/nwis_client/tests/test_nwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,17 @@ def test_get_raw_countyCd(setup_iv):
def test_splitting_bbox(test, validation):
v = iv._bbox_split(test)
assert v == validation


def test_get_returns_empty_canonical_dataframe(setup_iv, monkeypatch):
"""Verify that `get` can returns an empty canonical dataframe."""

def get_raw_mock(*args, **kwargs):
return [{"values": []}]

monkeypatch.setattr(iv.IVDataService, "get_raw", get_raw_mock)
df = setup_iv.get(
sites=["01189000"], startDt="2015-12-01T00:00", endDt="2015-12-31T23:45"
)
canonical_df = iv._create_empty_canonical_df()
assert df.equals(canonical_df)