Skip to content

Commit 03fa9cb

Browse files
committed
Made infl.get_cp() and infl.get_inflation()
1 parent b163210 commit 03fa9cb

5 files changed

Lines changed: 54 additions & 23 deletions

File tree

main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import ons
22

33

4-
val = ons.gdp.get_gdp()
4+
val = ons.infl.get_cp()
55
print(val)
66

7-
val = ons.gdp.get_infl_from_gdp(val)
7+
val = ons.infl.get_inflation()
88
print(val)
99

10-
print(ons.__version__)
10+

ons/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
from ons.request_data import get_data_frame
44
from ons.gdp import get_gdp
5+
from ons.infl import (get_cp, get_inflation)
56

67
__version__ = version("ons")

ons/gdp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
import ons
55

6+
base_key = "regional-gdp-by-quarter"
67
def get_gdp() -> pd.Series:
7-
jresp = ons.request_data.get_data_frame()
8+
jresp = ons.request_data.get_data_frame(key=base_key)
89
df = pd.read_csv(StringIO(jresp), engine='python', encoding='utf-8',
9-
usecols=["V4_1", "Time", "nuts", "sic-unofficial", "GrowthRate"],
10-
dtype={"V4_1": float},
10+
usecols=["v4_1", "Time", "nuts", "sic-unofficial", "GrowthRate"],
11+
dtype={"v4_1": float},
1112
parse_dates=["Time"],
1213
)
1314
df = df[df['nuts'] == 'UK0']

ons/infl.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pandas as pd
2+
from io import StringIO
3+
import ons
4+
5+
cp_add_str = "cpih01/editions/time-series/versions/29.csv"
6+
base_link = "https://api.beta.ons.gov.uk/v1/datasets/cpih01"
7+
key_cp = "cpih01"
8+
9+
def get_cp():
10+
jresp = ons.request_data.get_data_frame(key=key_cp)
11+
df = pd.read_csv(StringIO(jresp), engine='python', encoding='utf-8')
12+
df = df[df['Aggregate'] == 'Overall Index']
13+
df = df.iloc[:, [0, 1]]
14+
df.iloc[:, 1] = pd.to_datetime(df.iloc[:, 1], format='%b-%y')
15+
df.iloc[:, 0] = df.iloc[:, 0].astype('float', copy=False)
16+
df.set_index(list(df.columns[[1]]), inplace=True)
17+
df.sort_index(ascending=True, inplace=True)
18+
return df
19+
20+
def get_inflation():
21+
df = get_cp()
22+
df = df.iloc[:, 0].pct_change().round(4)
23+
df.dropna(inplace=True)
24+
return df
25+

ons/request_data.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import requests
2-
from requests import Response
2+
import json
33

4-
URL_BASE = "https://download.beta.ons.gov.uk/downloads/datasets/regional-gdp-by-quarter/editions/time-series/versions/4.csv"
54

5+
URL_BASE = "https://api.beta.ons.gov.uk/v1/datasets/"
66

7-
def get_data_frame(
8-
start_period: str = "1900-01-01",
9-
end_period: str = None,
10-
) -> str:
11-
request_url = URL_BASE
127

13-
try:
14-
abc: Response = requests.get(request_url)
15-
except requests.exceptions.HTTPError as err:
16-
raise requests.exceptions.HTTPError(
17-
f"HTTP error fetching data for {URL_BASE}:",
18-
abc.status_code,
19-
abc.reason,
20-
URL_BASE,
21-
) from err
22-
return abc.text
8+
def get_data_frame(key: str = "regional-gdp-by-quarter") :
9+
request_url = URL_BASE+key
10+
d = _connect_to_uk_api(request_url)
11+
url_ts = d['links']['latest_version']['href']
12+
csv_link = _connect_to_uk_api(url_ts)['downloads']['csv']['href']
13+
df = _connect_to_uk_api(csv_link, request_type='csv')
14+
return df
15+
16+
17+
def _connect_to_uk_api(url: str, request_type: str = 'json') -> dict:
18+
session = requests.session()
19+
r = session.get(url=url)
20+
if r.status_code != requests.codes.ok:
21+
raise Exception(r.status_code, r.reason, url)
22+
session.close()
23+
if request_type == 'json':
24+
return json.loads(r.text)
25+
elif request_type == 'csv':
26+
return r.text

0 commit comments

Comments
 (0)