Skip to content

Instantly share code, notes, and snippets.

@textbook

textbook/README.md

Created Oct 2, 2019
Embed
What would you like to do?
How to create a pandas Series of ONS CPIH data
import csv
from operator import itemgetter
import pandas as pd
import requests
cpih = requests.get("https://api.beta.ons.gov.uk/v1/datasets/cpih01").json()
latest = requests.get(cpih["links"]["latest_version"]["href"]).json()
data = requests.get(latest["downloads"]["csv"]["href"]).text
def tokenizer(s, c):
"""Like str.split, but an iterator.
Author: hughdbrown
Source: https://stackoverflow.com/a/4586090/3001761
"""
i = 0
while True:
try:
j = s.index(c, i)
except ValueError:
yield s[i:]
return
yield s[i:j]
i = j + 1
reader = csv.DictReader(tokenizer(data, "\n"))
overall = {
datetime.strptime(val["mmm-yy"], "%b-%y").date(): float(val["V4_0"])
for val in reader
if val["aggregate"] == "Overall Index"
}
consumer_price_index = pd.Series(
[val for _, val in sorted(overall.items(), key=itemgetter(0))],
index=pd.date_range(min(overall), periods=len(overall), freq="M")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment