Repository navigation
feat(ogc): accept date, datetime and Timestamp values in date arguments - #442
Merged
thodson-usgs merged 3 commits intoOct 12, 2026
Merged
Conversation
A datetime.date, datetime.datetime or pandas.Timestamp passed to a date
argument (time=[df.index.min(), None]) failed inside the formatter with
AttributeError ('endswith') or, for a lone datetime, TypeError (not
iterable), naming no argument. Read them like the equivalent string:
aware values convert to UTC, naive ones use the local zone as naive
strings do, a date is midnight of that day, and NaT is an open bound.
Any other type raises ValueError naming the argument.
Follow-up named in DOI-USGS#441.
Contributor
Author
|
@thodson-usgs this picks up the non-string follow-up you listed in #441. I went with accepting date/datetime/Timestamp rather than only rejecting them, since |
thodson-usgs
added a commit
to bhaskargurram-ai/dataretrieval-python
that referenced
this pull request
Oct 11, 2026
A tuple keeps _split_interval assignable to items once DOI-USGS#442 widens the element type, where a list[str | None] would trip list invariance.
This was referenced Oct 11, 2026
_validate_time_no_duration iterated a lone Timestamp/date and raised TypeError before the formatter could read it; it now normalizes with _coerce_to_list and checks only string elements. Also: - drop the redundant date check in _coerce_to_list (a date is not iterable), and update the Mapping TypeError to name the accepted types; - read a bytes value as one element, so it gets the message naming the accepted types rather than "takes at most 2 values" over its byte codes; - get_ratings' time docstring names the accepted date types; - tighten the NEWS entry and date it by its land, above DOI-USGS#446's. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HBt3Ee7eneKtdKQWq2T3YK
thodson-usgs
force-pushed
the
feat/ogc-dates-accept-datetime
branch
from
October 12, 2026 17:03
0783657 to
e2f8360
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up from #441, which noted that
pd.Timestampanddatetimeelements fail withAttributeErrororTypeError.Problem
A
datetime.date,datetime.datetimeorpandas.Timestamppassed to a date argument fails inside the formatter, and the error names no argument:time=[pd.Timestamp("2024-01-01", tz="UTC"), None]andtime=[date(2024, 1, 1), None]raiseAttributeError: ... has no attribute 'endswith'time=datetime(2024, 1, 1)raisesTypeError: 'datetime.datetime' object is not iterabletime=[df.index.min(), None]is a natural call for a pandas user, and today it hits the first case.Change
In
dataretrieval/ogc/dates.py, these values are now read like the equivalent string:dateis midnight of that day.NaTis an open bound, likeNone.A lone value is treated as a single element, as a lone string already is. Any other type, for example a number, raises
ValueErrornaming the argument and listing the accepted types (the minimum suggested in #441)._format_onenow delegates to a small_to_datetimehelper, so each function stays under the complexity limit.Tests
test_format_api_dates_accepts_date_and_datetime_objects: date, date pairs, awareTimestampanddatetime(an offset converted to UTC),NaTas an open bound, and mixed string andTimestampranges.test_format_api_dates_reads_naive_objects_like_naive_strings: a naivedatetime/Timestampand its string spelling build the same filter, so this holds in any time zone.test_format_api_dates_rejects_other_types_naming_the_argumenttest_construct_api_requests_accepts_timestamp_bounds: request-level check thattime=[Timestamp, None]producestime=2024-01-01%2F..The full suite passes locally (1267 passed), including the new tests under TZ=UTC, America/Denver, Asia/Kolkata and America/Chicago.
coverage reportpasses at 98.99%. ruff, mypy, lint-imports, xenon and complexipy are clean.Open question
The public getter annotations still say
str | list[str]. Should I widen them to include date objects in this PR, or leave that as a separate change?Maintainer update (2026-10-12)
The branch is now three commits: the original commit above, unchanged; a merge of
main; and one commit with the maintainer's edits.get_ratings.get_ratings(time=...)accepts a lonedateorTimestamp. Its duration check used to iterate the value and raiseTypeError.bytesvalues. Abytesvalue is read as one element, so it gets the message naming the accepted types instead of "takes at most 2 values" over its byte codes.get_ratings'timedocstring names the accepted date types.DateArgument), not here.Time zones: following pandas, #443 makes a naive date or time UTC rather than the machine's local time, so the "naive value is read in the local zone" line above holds only until #443 lands.
Merge this before #443. The full offline suite passes (1280 tests), and so do ruff, mypy and the pre-commit gates.