Last active
January 12, 2021 19:20
-
-
Save sambrightman/fd455b129da5730e72dd81bf79ae14a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests | |
pandas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import pandas | |
from urllib import parse | |
API_URL = 'https://coronavirus.data.gov.uk/api/' | |
INITIAL_PATH = '/v1/data?filters=areaType=nation&structure=%7B%22areaType%22:%22areaType%22,%22areaName%22:%22areaName%22,%22areaCode%22:%22areaCode%22,%22date%22:%22date%22,%22cumPeopleVaccinatedFirstDoseByPublishDate%22:%22cumPeopleVaccinatedFirstDoseByPublishDate%22,%22cumPeopleVaccinatedSecondDoseByPublishDate%22:%22cumPeopleVaccinatedSecondDoseByPublishDate%22%7D&format=json' | |
def fetch(path): | |
url = parse.urljoin(API_URL, path) | |
response = requests.get(url) | |
response.raise_for_status() | |
response_json = response.json() | |
return response_json['length'], response_json['data'], response_json['pagination']['current'], response_json['pagination']['last'], response_json['pagination']['next'] | |
def fetch_all(): | |
next_path = INITIAL_PATH | |
while True: | |
length, data, current_path, last_path, next_path = fetch(next_path[1:]) | |
if len(data) == length: | |
break | |
elif current_path == last_path: | |
raise Exception('reached end of pages') | |
elif next_path is None: | |
raise Exception('no next page') | |
return pandas.DataFrame(data).set_index('date').rename({ | |
'cumPeopleVaccinatedFirstDoseByPublishDate': 'cumFirstDoses', | |
'cumPeopleVaccinatedSecondDoseByPublishDate': 'cumSecondDoses', | |
}, axis='columns') | |
df = fetch_all() | |
df['cumTotal'] = df['cumFirstDoses'] + df['cumSecondDoses'] | |
grouped = df.groupby('date').sum() | |
diffs = grouped.diff().rename(lambda name: 'n{}'.format(name[1:]), axis='columns') | |
grouped.merge(diffs, on='date') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment