Python client for the UK Office for National Statistics (ONS) API. Fetches macroeconomic data (GDP, CPIH inflation) as pandas Series/DataFrames.
- Poetry for environment & dependency management.
poetry run python ...to run scripts.poetry addinstead ofpip install.
Any change to production code (new feature, bugfix, refactor, behavior change) must follow TDD: write a failing test first, then the minimal code that makes it pass.
Cycle: RED → verify RED → GREEN → verify GREEN → REFACTOR.
Rules:
- Tests run via:
poetry run pytest -q. - Before writing code, see the test fail for a real reason (
AssertionError/ missing function), not a typo/import error. - For bugfix: first a test reproducing the bug, then the fix.
- One test = one behavior. Test name describes the behavior meaningfully.
- After GREEN, run the full test suite to make sure nothing broke.
- Determine whether executable Python code was changed.
- If executable code was changed — run tests:
poetry run pytest -q. - If only comments or docstrings were changed — do not run tests.
- If tests fail, attempt to fix and re-run (max 2 retries), then stop and report.
- Before finishing any code change, run
poetry run ruff check .and fix every reported issue. If a warning is truly unavoidable, silence it with a targeted# noqa: <CODE>comment.
У ONS два публичных API. Оба без аутентификации.
Base URL: https://www.ons.gov.uk/economy/inflationandpriceindices/timeseries/{CDID}/{dataset}/data
Возвращает JSON с массивами months, quarters, years. Данные актуальные (обновляются вместе с сайтом ONS).
Используемые серии:
| CDID | Описание | Единица | Dataset |
|---|---|---|---|
| L522 | CPIH INDEX 00: ALL ITEMS 2015=100 | Index | mm23 |
| L55O | CPIH ANNUAL RATE 00: ALL ITEMS | % | mm23 |
| D7BT | CPI INDEX 00: ALL ITEMS 2015=100 | Index | mm23 |
Workflow: GET .../timeseries/l522/mm23/data → парсить response["months"], каждый элемент: {"date": "2026 APR", "value": "141.8"}.
Base URL: https://api.beta.ons.gov.uk/v1/
Ответы JSON. CSV — по ссылкам из метаданных версии.
| Endpoint | Description |
|---|---|
/datasets/{id} |
Метаданные датасета (title, contacts, editions) |
/datasets/{id}/editions |
Доступные editions (обычно time-series) |
/datasets/{id}/editions/{edition}/versions/{ver} |
Метаданные версии: dimensions, download links (CSV/XLS) |
/datasets/{id}/editions/{edition}/versions/{ver}/observations?dim=val&... |
Фильтрованные наблюдения. * — wildcard (макс. 10 000 строк) |
Workflow: GET /datasets/{id} → links.latest_version.href → downloads.csv.href → pandas.
cpih01 — CPIH (ранее использовался в get_cpih(), заменён на Website API 2026-05-27)
- Edition:
time-series, последняя версия в API: 67 (от 2026-02-18, данные по январь 2026) - Dimensions:
Time(mmm-yy),Geography(uk-only),Aggregate(cpih1dim1aggid) - Фильтр:
Aggregate == 'Overall Index' - Проблема: API перестал обновляться с февраля 2026, сайт ONS уходит вперёд на месяцы
- Для возврата: в
infl.pyзаменить вызовget_timeseries("l522")обратно наget_data("cpih01")и парсить CSV черезpd.read_csv(StringIO(response))с фильтром поAggregate - Docs: https://www.ons.gov.uk/economy/inflationandpriceindices/datasets/consumerpriceinflation
- API explorer: https://developer.ons.gov.uk/observations/
regional-gdp-by-quarter — Quarterly GDP for England, Wales and English regions
- Edition:
time-series, latest version: 6 - Dimensions:
Time(yyyy-qq),Geography(nuts),sic-unofficial(SIC codes),type-of-prices,quarterly-index-and-growth-rate - Фильтр:
nuts == 'UK0',sic-unofficial == 'A--T',GrowthRate == 'Quarterly index' - Last updated: 2023-05-24
- Contact: regionalgdp@ons.gov.uk
ons/— library source coderequest_data.py— HTTP client (Website API + Beta API)gdp.py— UK GDP quarterly indexinfl.py— CPIH index and inflation rate
tests/— pytest test suitemain.py— usage examples
- All code comments and docstrings in English.
- Type hints for all function parameters and return types.
- Minimum supported Python version: see
pyproject.toml(python = ">=3.14,<4.0.0"). - Use modern syntax: built-in generics (
list[int]), union syntax (X | None), literals over constructors. - Ruff configuration is in
pyproject.toml([tool.ruff.lint], selectingC,E,F,W,B). Treat it as the authoritative style guide.