Skip to content

feat(waterdata): add getters for time-series revisions and methods - #471

Open
thodson-usgs wants to merge 11 commits into
DOI-USGS:mainfrom
thodson-usgs:feat/time-series-revisions-methods
Open

thodson-usgs wants to merge 11 commits into
DOI-USGS:mainfrom
thodson-usgs:feat/time-series-revisions-methods

Conversation

@thodson-usgs

@thodson-usgs thodson-usgs commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Adds typed getters for two Water Data OGC collections that dataretrieval did not wrap: time-series-revisions and time-series-methods.

Usage

from dataretrieval import waterdata

# Why approved data at a location were revised, for intervals that began
# from 2018-01-01 to 2019-12-31T00:00Z (a date-only bound is midnight UTC)
revisions, _ = waterdata.get_time_series_revisions(
    monitoring_location_id="USGS-01646500",
    begin="2018-01-01/2019-12-31",
)
# 23 rows: monitoring_location_id, time_series_id, revision_note, begin, end,
#          time_series_revision_id

# Which method was in effect for one time series, interval by interval.
# This collection has no monitoring_location_id filter; get the series IDs
# from get_time_series_metadata() first.
methods, _ = waterdata.get_time_series_methods(
    time_series_id="0003587a6a544f6aa37fe13dc2050985"
)
# 10 rows: time_series_id, begin, end, method_id, method_category,
#          time_series_method_id

Both return a plain pandas.DataFrame. Once #470 is merged, method_id joins to waterdata.get_reference_table("methods") for each method's name and description.

New functions

Both are in dataretrieval.waterdata.metadata, exported from dataretrieval.waterdata and re-exported by the waterdata.api facade.

get_time_series_revisions(
    monitoring_location_id=None, time_series_id=None, time_series_revision_id=None,
    revision_note=None, begin=None, end=None,
    properties=None, limit=None, filter=None, filter_lang=None,
    convert_type=True, max_rows=None, **queryables,
)

get_time_series_methods(
    time_series_id=None, time_series_method_id=None, method_id=None,
    method_category=None, begin=None, end=None,
    properties=None, limit=None, filter=None, filter_lang=None,
    convert_type=True, max_rows=None, **queryables,
)

Each named argument is one of the collection's queryables, plus the control arguments the other getters share. Both collections are non-spatial: the server returns geometry: null, and returns HTTP 400 for bbox. So neither getter has bbox or skip_geometry. Both collections are listed in a new _NONSPATIAL_COLLECTIONS set in waterdata/utils.py, and the spatial argument of waterdata.utils.get_ogc_data now defaults to None, meaning it is looked up in that set. As a result both getters return a plain DataFrame even when geopandas is installed, and so does get_cql on either collection. Reference tables still pass spatial=False explicitly. time-series-methods has no monitoring_location_id queryable (the server returns HTTP 400 for it), so get_time_series_methods doesn't offer that argument, and its docstring points to get_time_series_metadata for looking up a location's time_series_id values.

ID columns

The id the server sends is returned as time_series_revision_id and time_series_method_id. These follow the <collection, singular>_id pattern used by daily_id, peak_id and field_measurement_id. Both are in _OUTPUT_ID_BY_COLLECTION and WATERDATA_COLLECTIONS, so get_cql and get_queryables accept the collections. Because both are synthetic IDs, _EXTRA_ID_COLS moves them to the end of the frame.

Date parsing fix

time-series-methods sends end with mixed second precision: closed intervals end at ...:00.000001+00:00, some at ...:00+00:00, and open intervals at 9999-12-31T23:59:59.999999+00:00. _type_cols called pd.to_datetime with no format. pandas infers a single format from the first value, so every value of the other precision became NaT with a UserWarning. Live, one series lost 1 of its 10 end values. _type_cols now parses as ISO 8601. If a column mixes naive and offset values, the ISO 8601 parse raises instead of coercing, so in that case the column is parsed with the inferred format, as it was before. pandas 2 parses to nanoseconds, which span 1677 to 2262, so it turns the 0001-01-01 and 9999-12-31 bounds into NaT. On pandas 2 a column that lost a value this way is parsed again at microseconds, and every other parsed time column is converted to microseconds, the resolution pandas 3 uses. Without the conversion, end was at microseconds and begin at nanoseconds, so end - begin and a pd.concat with a closed-interval frame raised OutOfBoundsDatetime. This changes the shared OGC shaping for all Water Data and NGWMN getters. I checked get_daily, get_continuous, get_field_measurements, get_time_series_metadata and ngwmn.get_water_level live with UserWarnings as errors: dtypes are unchanged and no value became NaT. The live suite passes too. On pandas 2 (the Python 3.10 CI job), the time columns of every Water Data and NGWMN getter change from nanoseconds to microseconds; NEWS lists this as a behavior change.

Demo notebook

The introduction notebook has a new section, Time-series revisions and methods. It gets the revisions for one monitoring location, and the methods for another location's discharge and temperature series, joined to the series metadata.

Verification

  • Gates: ruff check, ruff format --check, mypy, lint-imports (7 kept), xenon, complexipy, and coverage run -m pytest tests/ && coverage report (1710 passed, 99.06% branch coverage, above the ratchet) all pass. The offline suite also passes on pandas 2.3.3, except for the tests that need geopandas, which that environment did not have.
  • Offline tests use the existing waterdata_ogc_fixtures.json style, with two real features per collection added. They assert the exact query string sent (including begin rendered as 2018-01-01T00:00:00Z/2019-12-31T00:00:00Z), a CQL2 in for a list of time_series_id, a plain DataFrame with no geometry, the renamed ID columns, and that the 9999-12-31 end is kept after parsing. waterdata_queryables.json was regenerated with the script in waterdata_queryables_test.py; only the two new collections changed. Both getters are registered in the properties and public-API contract tests.
  • Live (pytest -m live tests/, 53 passed). Direct calls:
    • get_time_series_revisions(monitoring_location_id="USGS-01646500"): 26 rows. With begin="2018-01-01/2019-12-31": 23 rows.
    • get_time_series_methods(time_series_id="0003587a6a544f6aa37fe13dc2050985") (USGS-05124000 water temperature): 10 rows, no NaT in end. With begin="2021-01-01/2021-12-31": 7 rows.

thodson-usgs and others added 7 commits November 18, 2026 10:44
Add get_time_series_revisions and get_time_series_methods for the
time-series-revisions and time-series-methods OGC collections. Both
collections are non-spatial, so the getters omit bbox and skip_geometry
and pass spatial=False. Register both in _OUTPUT_ID_BY_COLLECTION
(time_series_revision_id, time_series_method_id) and
WATERDATA_COLLECTIONS so get_cql and get_queryables accept them.

Parse OGC datetime columns as ISO 8601. time-series-methods mixes whole
and fractional seconds in end, and the inferred format turned every
value of the other precision into NaT.
Mock the empty-result test with the existing _mock_items and _schema_url
helpers instead of a regex that matched both collections' items and
schema URLs, and assert the empty frame drops the schema's geometry.
Drop the collection name from the ISO 8601 comment in the shared OGC
shaping code, which names no service.
format="ISO8601" raises, rather than coercing, when a column mixes
naive and offset values, where the inferred parse it replaced returned
the column with the odd values as NaT. Fall back to the inferred parse
in that case, so a getter that returned such a column before still
does.
…as 2

The Python 3.10 CI legs run pandas 2.3, the last pandas for 3.10, and
failed four tests.

When a column mixes naive and offset values, pandas 2 parses it with
format="ISO8601" by returning object values and a FutureWarning, where
pandas 3 raises. _parse_datetimes caught only the raise, so on pandas 2
the column came back as object dtype instead of datetimes. A result that
is not a datetime dtype now falls back to the inferred parse too, and the
ISO 8601 attempt's FutureWarning is suppressed (the fallback repeats it
where it applies).

pandas 2 parses to nanoseconds, which end in 2262, so the 9999-12-31 open
interval end is NaT there. The tests that check it is kept now run on
pandas 3 only, and the revisions test checks for a UTC datetime dtype
rather than the microsecond unit pandas 3 uses.
… frames

On pandas 2, which parses datetimes to nanoseconds (1677 to 2262),
time-series-methods' 0001-01-01 start and 9999-12-31 end of an open
interval became NaT with a "could not be parsed" warning, and a column
holding only such values lost its time zone. When the ISO 8601 parse
loses a value, the column is parsed again at microsecond resolution,
which pandas 3 uses already.

get_cql on time-series-methods or time-series-revisions returned a
GeoDataFrame with a column of null geometries, unlike the typed getters.
The Water Data wrapper now treats both collections as non-spatial for
every caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The microsecond re-parse ran whenever a value was lost, on either
pandas, and astype accepts formats ISO 8601 does not, so on pandas 3 a
column with "01/02/2024" was parsed without the unparsed-value warning.
It now runs only where the loss can come from the nanosecond range: on
pandas 2, and only for a column with NaT, which also skips the mask on
every clean column.

get_ogc_data's spatial argument defaults from _NONSPATIAL_COLLECTIONS,
as output_id defaults from _OUTPUT_ID_BY_COLLECTION, instead of the
set overriding whatever the caller passed.

Docstrings and the NEWS sentence keep the constraint, not the history;
the open-interval test is folded into test_get_time_series_methods.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thodson-usgs thodson-usgs added the v1 Non-breaking: can merge for the next 1.x release label Nov 23, 2026
…n notebook

AGENTS.md asks for a demo with each new collection. Both collections describe
a time series, so they get a short section after the combined-metadata example
rather than a notebook of their own.
@thodson-usgs
thodson-usgs marked this pull request as ready for review November 30, 2026 18:18
@thodson-usgs
thodson-usgs requested a review from ehinman December 1, 2026 15:37
thodson-usgs and others added 3 commits December 1, 2026 13:35
Docstrings and comments said records "carry" no geometry, that a column
"takes the inferred parse", that the `spatial` default "reads it from"
a set for collections "outside the map", and that a fallback "keeps the
call returning, with the odd value NaT". They now say what is computed:
records have no geometry, `spatial=None` means False for a collection
in `_NONSPATIAL_COLLECTIONS` and True otherwise, and a mixed naive and
offset column is parsed with the inferred format, with NaT for the
offset value. NEWS and the notebook say the collections "contain" the
data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pandas 2 parses datetimes to nanoseconds. Only a column that lost an
out-of-range value (0001-01-01 or 9999-12-31) was parsed again at
microseconds, so a `time-series-methods` frame with an open interval had
`end` at microseconds and `begin` at nanoseconds. `end - begin`, and a
concat with a frame that had no open interval, raised
OutOfBoundsDatetime. Every parsed time column is now converted to
microseconds on pandas 2, the resolution pandas 3 uses, so a column's
resolution no longer depends on its values. NEWS lists it as a behavior
change for pandas 2.

Also from review:
- The `_parse_datetimes` docstring says the inferred parse raises on
  pandas 3 for a column that mixes UTC offsets, as it did before.
- The docstring examples say a date-only end bound is midnight UTC, in
  place of "began in 2018 or 2019".
- The NEWS entry moves to the top, dated 12/01/2026, to be re-dated on
  the day it lands, and is shorter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Non-breaking: can merge for the next 1.x release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant