Skip to content

Instantly share code, notes, and snippets.



Created Oct 2, 2019
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("").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
i = 0
while True:
j = s.index(c, i)
except ValueError:
yield s[i:]
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