bolster.data_sources.ons_cpi
ONS UK inflation indices (CPI, CPIH, RPI).
Wraps the Office for National Statistics (ONS) open time-series API to provide standardised access to the headline UK inflation measures:
CPIH — Consumer Prices Index including owner-occupiers’ housing costs
CPI — Consumer Prices Index
RPI — Retail Prices Index (longest run, back to 1948)
Each measure is available both as a 12-month annual rate (%) and as a price
index. Data is published at monthly, quarterly and annual resolutions.
This is the first of three macroeconomic context modules and emits a fixed output schema so that inflation, and the other macro modules, can be joined and resampled against one another:
Column |
Description |
|---|---|
date |
|
year |
|
quarter |
|
month |
|
resolution |
|
series |
|
value |
|
unit |
|
geography |
|
source |
|
Example
>>> from bolster.data_sources import ons_cpi
>>> df = ons_cpi.get_series("D7G7", resolution="annual")
>>> sorted(df.columns)
['date', 'geography', 'month', 'quarter', 'resolution', 'series', 'source', 'unit', 'value', 'year']
Attributes
Exceptions
Base exception for ONS data errors. |
|
Raised when a DataFrame fails |
Functions
|
Fetch a single ONS inflation series at a given resolution. |
|
Fetch all supported series at a given resolution, concatenated. |
|
Validate that a DataFrame conforms to the macroeconomic schema. |
Module Contents
- bolster.data_sources.ons_cpi.BASE_URL = 'https://www.ons.gov.uk/economy/inflationandpriceindices/timeseries/{code}/data'[source]
- bolster.data_sources.ons_cpi.SCHEMA_COLUMNS = ['date', 'year', 'quarter', 'month', 'resolution', 'series', 'value', 'unit', 'geography', 'source'][source]
- exception bolster.data_sources.ons_cpi.ONSDataError[source]
Bases:
ExceptionBase exception for ONS data errors.
Initialize self. See help(type(self)) for accurate signature.
- exception bolster.data_sources.ons_cpi.ONSValidationError[source]
Bases:
ONSDataErrorRaised when a DataFrame fails
validate_data().Initialize self. See help(type(self)) for accurate signature.
- bolster.data_sources.ons_cpi.get_series(code, resolution='monthly', force_refresh=False)[source]
Fetch a single ONS inflation series at a given resolution.
- Parameters:
- Returns:
DataFrame conforming to
SCHEMA_COLUMNS, sorted bydate.- Raises:
ValueError – If
codeorresolutionis not recognised.ONSDataError – If the underlying API request fails.
- Return type:
Example
>>> df = get_series("D7G7", resolution="annual") >>> df.iloc[-1][["series", "unit", "geography"]].tolist() ['D7G7', '%', 'UK']
- bolster.data_sources.ons_cpi.get_latest_data(resolution='monthly', force_refresh=False)[source]
Fetch all supported series at a given resolution, concatenated.
- Parameters:
- Returns:
DataFrame conforming to
SCHEMA_COLUMNScontaining every code inSERIES, sorted byseriesthendate.- Raises:
ValueError – If
resolutionis not recognised.- Return type:
Example
>>> df = get_latest_data(resolution="annual") >>> sorted(df["series"].unique()) ['CHAW', 'CZBH', 'D7BT', 'D7G7', 'L522', 'L55O']
- bolster.data_sources.ons_cpi.validate_data(df)[source]
Validate that a DataFrame conforms to the macroeconomic schema.
Checks performed:
All
SCHEMA_COLUMNSare present.At least one row is present.
geographyis exclusively “UK” andsourceexclusively “ONS”.resolutiononly contains known values.valueis numeric with no nulls.
- Parameters:
df (pandas.DataFrame) – DataFrame to validate.
- Returns:
Trueif all checks pass.- Raises:
ONSValidationError – If any check fails.
- Return type:
Example
>>> df = get_series("D7G7", resolution="annual") >>> validate_data(df) True