USGS dataretrieval Python Package get_iv() Examples
This notebook provides examples of using the Python dataretrieval package to retrieve instantaneous values data for a United States Geological Survey (USGS) monitoring site. The dataretrieval package provides a collection of functions to get data from the USGS National Water Information System (NWIS) and other online sources of hydrology and water quality data, including the United States Environmental Protection Agency (USEPA).
Install the Package
Use the following code to install the package if it doesn’t exist already within your Jupyter Python environment.
[1]:
!pip install dataretrieval
Requirement already satisfied: dataretrieval in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (0.1.dev1+g0aec2c864)
Requirement already satisfied: requests in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from dataretrieval) (2.33.1)
Requirement already satisfied: pandas<4.0.0,>=2.0.0 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from dataretrieval) (3.0.2)
Requirement already satisfied: numpy>=1.26.0 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from pandas<4.0.0,>=2.0.0->dataretrieval) (2.4.4)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from pandas<4.0.0,>=2.0.0->dataretrieval) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas<4.0.0,>=2.0.0->dataretrieval) (1.17.0)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from requests->dataretrieval) (3.4.7)
Requirement already satisfied: idna<4,>=2.5 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from requests->dataretrieval) (3.11)
Requirement already satisfied: urllib3<3,>=1.26 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from requests->dataretrieval) (2.6.3)
Requirement already satisfied: certifi>=2023.5.7 in /opt/hostedtoolcache/Python/3.13.12/x64/lib/python3.13/site-packages (from requests->dataretrieval) (2026.2.25)
Load the package so you can use it along with other packages used in this notebook.
[2]:
from datetime import date
from IPython.display import display
from dataretrieval import nwis
import dataretrieval.waterdata as waterdata
Basic Usage
The dataretrieval package has several functions that allow you to retrieve data from different web services. This example uses the get_iv() function to retrieve instantaneous streamflow data for a USGS monitoring site from NWIS. The following arguments are supported:
sites (string or list of strings): A list of USGS site identifiers for which to retrieve data.
parameterCd (string or list of strings): A list of USGS parameter codes for which to retrieve data.
start (string): The beginning date for a period for which to retrieve data. If the waterdata parameter startDt is supplied, it will overwrite the start parameter.
end (string): The ending date for a period for which to retrieve data. If the waterdata parameter endDt is supplied, it will overwrite the end parameter.
Example 1: Get unit value data for a specific parameter at a USGS NWIS monitoring site between a begin and end date
[3]:
# Set the parameters needed for the web service call
siteID = "10109000" # LOGAN RIVER ABOVE STATE DAM, NEAR LOGAN, UT
parameterCode = "00060" # Discharge
startDate = "2021-09-01"
endDate = "2021-09-30"
# Get the data
discharge = waterdata.get_continuous(
monitoring_location_id=siteID, parameter_code=parameterCode, time=f"{startDate}/{endDate}"
)
print("Retrieved " + str(len(discharge[0])) + " data values.")
Retrieved 0 data values.
Interpreting the Result
The result of calling the get_iv() function is an object that contains a Pandas data frame object and an associated metadata object. The Pandas data frame contains the values for the observed variable and time period requested. The data frame is indexed by the dates associated with the data values.
Once you’ve got the data frame, there’s several useful things you can do to explore the data.
[4]:
# Display the data frame as a table
display(discharge[0])
| geometry | time_series_id | monitoring_location_id | parameter_code | statistic_id | time | value | unit_of_measure | approval_status | qualifier | last_modified | continuous_id |
|---|
Show the data types of the columns in the resulting data frame.
[5]:
print(discharge[0].dtypes)
geometry object
time_series_id object
monitoring_location_id object
parameter_code object
statistic_id object
time datetime64[s]
value int64
unit_of_measure object
approval_status object
qualifier object
last_modified datetime64[s]
continuous_id object
dtype: object
Get summary statistics for the daily streamflow values.
[6]:
discharge[0].describe()
[6]:
| time | value | last_modified | |
|---|---|---|---|
| count | 0 | 0.0 | 0 |
| mean | NaT | NaN | NaT |
| min | NaT | NaN | NaT |
| 25% | NaT | NaN | NaT |
| 50% | NaT | NaN | NaT |
| 75% | NaT | NaN | NaT |
| max | NaT | NaN | NaT |
| std | NaN | NaN | NaN |
Make a quick time series plot.
[7]:
ax = discharge[0].plot(x="time", y="value", style=".")
ax.set_xlabel("Date")
ax.set_ylabel("Streamflow (cfs)")
[7]:
Text(0, 0.5, 'Streamflow (cfs)')
The other part of the result returned from the get_iv() function is a metadata object that contains information about the query that was executed to return the data. For example, you can access the URL that was assembled to retrieve the requested data from the USGS web service. The USGS web service responses contain a descriptive header that defines and can be helpful in interpreting the contents of the response.
[8]:
print("The query URL used to retrieve the data from NWIS was: " + discharge[1].url)
The query URL used to retrieve the data from NWIS was: https://api.waterdata.usgs.gov/ogcapi/v0/collections/continuous/items?monitoring_location_id=10109000¶meter_code=00060&time=2021-09-01%2F2021-09-30&skipGeometry=False&limit=50000
Additional Examples
Example 2: Get unit values for an individual site and parameter between a start and end date.
NOTE: By default, start and end date are evaluated as local time, and the result is returned with the timestamps in the local time of the monitoring site.
[9]:
site_id = "05114000"
startDate = "2014-10-10"
endDate = "2014-10-10"
discharge2 = waterdata.get_continuous(
monitoring_location_id=site_id, parameter_code=parameterCode, time=f"{startDate}/{endDate}"
)
print("Retrieved " + str(len(discharge2[0])) + " data values.")
display(discharge2[0])
Retrieved 0 data values.
| geometry | time_series_id | monitoring_location_id | parameter_code | statistic_id | time | value | unit_of_measure | approval_status | qualifier | last_modified | continuous_id |
|---|
Example 3: Get unit values for an individual site for today
[10]:
today = str(date.today())
discharge_today = waterdata.get_continuous(
monitoring_location_id=site_id, parameter_code=parameterCode, time=f"{today}/{today}"
)
print("Retrieved " + str(len(discharge_today[0])) + " data values.")
display(discharge_today[0])
Retrieved 0 data values.
| geometry | time_series_id | monitoring_location_id | parameter_code | statistic_id | time | value | unit_of_measure | approval_status | qualifier | last_modified | continuous_id |
|---|
Example 4: Retrieve data using UTC times
NOTE: Adding ‘Z’ to the input time parameters indicates that they are in UTC rather than local time. The time stamps associated with the data returned are still in the local time of the USGS monitoring site.
[11]:
discharge_UTC = waterdata.get_continuous(
monitoring_location_id=site_id,
parameter_code=parameterCode,
time="2014-10-10T00:00Z/2014-10-10T23:59Z",
)
print("Retrieved " + str(len(discharge_UTC[0])) + " data values.")
display(discharge_UTC[0])
Retrieved 0 data values.
| geometry | time_series_id | monitoring_location_id | parameter_code | statistic_id | time | value | unit_of_measure | approval_status | qualifier | last_modified | continuous_id |
|---|
Example 5: Get unit values for two sites, for a single parameter, between a start and end date
[12]:
discharge_multisite = waterdata.get_continuous(
monitoring_location_id=["04024430", "04024000"],
parameter_code=parameterCode,
time="2013-10-01/2013-10-01",
)
print("Retrieved " + str(len(discharge_multisite[0])) + " data values.")
display(discharge_multisite[0])
Retrieved 0 data values.
| geometry | time_series_id | monitoring_location_id | parameter_code | statistic_id | time | value | unit_of_measure | approval_status | qualifier | last_modified | continuous_id |
|---|
The following example is the same as the previous example but with multi index turned off (multi_index=False)
[13]:
discharge_multisite = waterdata.get_continuous(
monitoring_location_id=["04024430", "04024000"],
parameter_code=parameterCode,
time="2013-10-01/2013-10-01",
)
print("Retrieved " + str(len(discharge_multisite[0])) + " data values.")
display(discharge_multisite[0])
Retrieved 0 data values.
| geometry | time_series_id | monitoring_location_id | parameter_code | statistic_id | time | value | unit_of_measure | approval_status | qualifier | last_modified | continuous_id |
|---|