Skip to main content

A convenient interface for USGS WaterData APIs.

Project description

OWPHydroTools :: WaterData Client

This subpackage implements an interface to the USGS WaterData APIs. The primary use for this tool is to populate pandas.Dataframe objects with USGS streamflow data. See the WaterData Client Documentation for a complete list and description of the currently available methods. To report bugs or request new features, submit an issue through the OWPHydroTools Issue Tracker on GitHub.

Installation

In accordance with the python community, we support and advise the usage of virtual environments in any workflow using python. In the following installation guide, we use python's built-in venv module to create a virtual environment in which the tool will be installed. Note this is just personal preference, any python virtual environment manager should work just fine (conda, pipenv, etc. ).

# Create and activate python environment, requires python >= 3.11
$ python3 -m venv venv
$ source venv/bin/activate
$ python3 -m pip install --upgrade pip

# Install waterdata_client
$ python3 -m pip install hydrotools.waterdata_client

Usage

The following examples demonstrate typical usage of hydrotools.waterdata_client.

Retrieve most recent streamflow values

# The LatestContinuousClient provides an interface to the latest-continuous endpoint.
from hydrotools.waterdata_client import LatestContinuousClient

# The default client returns deserialized JSON as a list of Python dictionaries.
#   We'll import a transformer that converts these data to a GeoDataFrame.
from hydrotools.waterdata_client.transformers import to_geodataframe

# Instantiate the client with the GeoDataFrame transformer
client = LatestContinuousClient(transformer=to_geodataframe)

# Retrieve data
observations = client.get(
    monitoring_location_id="USGS-02146470",
    parameter_code="00060" # Volumetric streamflow in ft^3/s
    )

# Look at values
print(observations[["usgs_site_code", "value_time", "value", "geometry"]])
print(observations.columns)

Output

  usgs_site_code                 value_time value                    geometry
0  USGS-02146470  2026-05-18T18:45:00+00:00  0.40  POINT (-80.85306 35.16444)

Index(['geometry', 'id', 'time_series_id', 'usgs_site_code', 'parameter_code',
       'statistic_id', 'value_time', 'value', 'measurement_unit',
       'approval_status', 'qualifiers', 'last_modified'],
      dtype='str')

Retrieve values using datetime objects

import pandas as pd
# from datetime import datetime, timedelta
# from zoneinfo import ZoneInfo

# The ContinuousClient provides an interface to the continuous endpoint.
from hydrotools.waterdata_client import ContinuousClient

# The default client returns deserialized JSON as a list of Python dictionaries.
#   We'll import a transformer that converts these data to a DataFrame.
from hydrotools.waterdata_client.transformers import to_dataframe

# Instantiate the client with the DataFrame transformer
client = ContinuousClient(transformer=to_dataframe)

# Retrieve data using datetime handling
observations = client.get(
    monitoring_location_id="USGS-02146470",
    limit=50_000,
    parameter_code="00065", # Stage in ft.

    # Strings are passed directly to the API without intervention. See the API
    #   documentation for more information about valid string formats (RFC 3339).
    # datetime="2025-05-03T12:00:00-05:00/2025-05-05T00:00:00-05:00",

    # Retrieve the last 6 hours using pandas.Timedelta
    datetime=pd.Timedelta(hours=6),

    # Retrieve a specific datetime using pandas.Timestamp
    #   Timezone naive objects are assumed to be UTC.
    # datetime=pd.Timestamp("2010-01-01"),

    # Timezone aware types are converted to UTC before retrieval.
    # datetime=pd.Timestamp("2010-01-01", tzinfo=ZoneInfo("America/Chicago")),

    # Retrieve using a standard datetime object
    # datetime=datetime(2026, 1, 1, 0, 30, 0),

    # Retrieve using a standard timedelta object
    # datetime=timedelta(hours=6),

    # Use a list of datetime objects to specify a bounded interval [start, end]
    # datetime=[pd.Timestamp("2026-01-01"), pd.Timestamp("2026-01-02")],

    # Specify a half-bounded interval using None
    # datetime=[pd.Timestamp("2026-05-01"), None],
    )

# Look at values
print(observations[["usgs_site_code", "value_time", "value", "geometry"]])
print(observations.columns)

Output

   usgs_site_code                 value_time value geometry
0   USGS-02146470  2026-05-18T13:00:00+00:00  3.07     None
1   USGS-02146470  2026-05-18T13:05:00+00:00  3.07     None
2   USGS-02146470  2026-05-18T13:10:00+00:00  3.07     None
3   USGS-02146470  2026-05-18T13:15:00+00:00  3.08     None
4   USGS-02146470  2026-05-18T13:20:00+00:00  3.08     None
..            ...                        ...   ...      ...
66  USGS-02146470  2026-05-18T18:30:00+00:00  3.08     None
67  USGS-02146470  2026-05-18T18:35:00+00:00  3.08     None
68  USGS-02146470  2026-05-18T18:40:00+00:00  3.08     None
69  USGS-02146470  2026-05-18T18:45:00+00:00  3.08     None
70  USGS-02146470  2026-05-18T18:50:00+00:00  3.08     None

[71 rows x 4 columns]
Index(['type', 'id', 'geometry', 'geo_feature_id', 'time_series_id',
       'usgs_site_code', 'parameter_code', 'statistic_id', 'value_time',
       'value', 'measurement_unit', 'approval_status', 'qualifiers',
       'last_modified'],
      dtype='str')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hydrotools_waterdata_client-0.8.1a0.tar.gz (90.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hydrotools_waterdata_client-0.8.1a0-py3-none-any.whl (208.3 kB view details)

Uploaded Python 3

File details

Details for the file hydrotools_waterdata_client-0.8.1a0.tar.gz.

File metadata

File hashes

Hashes for hydrotools_waterdata_client-0.8.1a0.tar.gz
Algorithm Hash digest
SHA256 a3412a07d532e3abc9787d1dff8c759b418c353cdc50fc4280e0d84afcd65a3b
MD5 ff6e26445203f9674c51fca0fbe8bb69
BLAKE2b-256 a018172be51b63ad114ecbec7f411a6c221adb8cc0a47f19bdaeab41a826eeb9

See more details on using hashes here.

File details

Details for the file hydrotools_waterdata_client-0.8.1a0-py3-none-any.whl.

File metadata

File hashes

Hashes for hydrotools_waterdata_client-0.8.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 55ab71f7e74e969ce3fabe6b632b8c3e947446ad43414d2bdeacd9d64693be8c
MD5 ace15736a18e4dc3dee5c93810be26ab
BLAKE2b-256 6139c069e00ec0134ea4bf769a241f6995fe3e03a0ea9c8ce96ff10e0a9f0f8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page