Skip to content

Instantly share code, notes, and snippets.

@textbook
Created October 2, 2019 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save textbook/c8f8aa79b01acdb33f465092ab818efe to your computer and use it in GitHub Desktop.
Save textbook/c8f8aa79b01acdb33f465092ab818efe to your computer and use it in GitHub Desktop.
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