feat(fred): get_observations, aligned and downsampled - #38
Merged
Conversation
The tool the server exists for, and where the token argument is cashed.
Three things it does that a wrapper over /series/observations does not:
Columnar output. FRED sends {realtime_start, realtime_end, date, value} per
observation, and the two realtime fields hold the same value on every row.
Measured on 20 years of DFF: 694,388 chars of raw payload become 2,856. Several
series share one date index, so a comparison is one call and arrives aligned,
with nulls (not forward-fill) where a quarterly series has no monthly print.
Summary before downsampling. latest, prior, change, min, max, mean and count are
computed over every observation; only the point list is thinned to max_points.
The same DFF call returns 120 of 7,305 points and still reports the true 20-year
min of 0.04 and max of 5.41. The fixture puts both extremes in the interior of
the series precisely so this test can fail if the order is ever reversed.
Argument correction. units="yoy" becomes pc1, start="5y" and "2020" and
"18 months" become real dates, frequency="monthly" becomes m. None of these are
guessable and all of them are unambiguous, so they are translated rather than
returned as an HTTP 400 about a variable name.
A bad series ID among good ones costs only its own column.
walkerhughes
force-pushed
the
fred/observations
branch
from
August 6, 2026 01:19
686fefa to
1030ca2
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.
Closes #33. Third of the
fredstack (#30), on top of #37.The tool the server exists for. Three things it does that a wrapper over
/series/observationsdoes not.1. Columnar output
FRED sends one object per observation:
{"realtime_start":"2026-08-05","realtime_end":"2026-08-05","date":"2025-01-01","value":"2.99098"}On a normal request the two realtime fields carry the same value on every row, and the key names repeat once per observation. So the response becomes:
{"dates": ["2025-01-01", ...], "values": {"UNRATE": [4.0, ...], "CPIAUCSL": [2.99, ...]}}Measured live on 20 years of
DFF:Several series share one date index, so a comparison is a single call and arrives already joined. Where a quarterly series has no monthly observation the value is
null, not forward-filled: filling would invent numbers FRED never published.2. Summary computed before downsampling
This is the part that makes long daily series usable, and the ordering is the whole trick. The fetch is unlimited and
latest,prior,change,pct_change,min,max,meanandcountare computed over every observation. Only the returned point list is thinned, to evenly spaced samples that always keep the first and last.120 points, and the true 20-year range. A summary computed after thinning would quietly report the sample's range as the series' range, and nothing about the output would look wrong.
The daily test fixture is built from explicit knots that put the peak and the trough in the interior of the series, away from the endpoints downsampling always keeps. With extremes at the endpoints the test could not tell the two orderings apart.
Nothing is silently truncated:
pointsreportstotalanddropped, with a note when thinning happened.3. Argument correction
None of FRED's vocabulary is guessable, and all of these inputs are unambiguous, so they are translated rather than answered with a 400 about a variable name.
units="yoy","year over year","inflation"pc1units="percent change","mom"pchunits="annualized","saar"pcaunits="level","raw",""linstart="5y","last 5 years","18 months"start="2020","2020-01"2020-01-01frequency="monthly"maggregation_method="total"sumThe response echoes
unitswith a plain-Englishunits_meaning, so the numbers are not left to be interpreted.Date arithmetic is calendar-correct without a dependency: three months before 31 May is 28 February, and a year before 29 Feb 2024 is 28 Feb 2023. Both are tests.
2020-13-01and a backwards range are caught here rather than at the API.Errors
A bad ID among good ones costs only its own column:
{"values": {"UNRATE": [...]}, "errors": {"NOPE": "Bad Request. The series does not exist."}}Invalid units, unreadable dates, and reversed ranges never reach the API at all; the tests assert
not fred.requests.Verification
243 tests, 98% coverage,
make checkclean.Live against the real API: