Last active
November 5, 2021 23:43
-
-
Save Ferdi265/0d7f2ff23edaccd89f03e2804a96dd96 to your computer and use it in GitHub Desktop.
Our World in Data - Compare Covid Data across Years
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
#!/usr/bin/env python3 | |
import sys | |
import matplotlib.pyplot as plt | |
import requests as rq | |
import pandas as pd | |
import numpy as np | |
from typing import cast | |
from io import StringIO | |
from datetime import date | |
def usage(): | |
print(f"usage: covid.py [--help|--list-columns] <country> [column='{DEFAULT_COLUMN}']") | |
sys.exit(1) | |
DEFAULT_COLUMN = "new_cases_smoothed" | |
args = sys.argv[1:] | |
action = "country" | |
while len(args) > 0 and args[0].startswith("-"): | |
if args[0] == "--help": | |
usage() | |
elif args[0] == "--list-columns": | |
action = "list-columns" | |
else: | |
print(f"error: invalid option '{args[0]}'") | |
sys.exit(1) | |
args = args[1:] | |
if action == "country" and len(args) != 1 and len(args) != 2: | |
usage() | |
elif action == "list-columns" and len(args) != 0: | |
usage() | |
print(">> Downloading Covid Data") | |
r = rq.get("https://covid.ourworldindata.org/data/owid-covid-data.csv") | |
if r.status_code != 200: | |
raise ValueError(f"HTTP error {r.status_code}") | |
print(">> Parsing CSV") | |
df = cast(pd.DataFrame, pd.read_csv(StringIO(r.text))) | |
if action == "country": | |
country = args[0] | |
if len(args) == 2: | |
column = args[1] | |
else: | |
column = DEFAULT_COLUMN | |
if country not in df.location.iloc: | |
print(f"error: invalid country '{country}'") | |
sys.exit(1) | |
df = df[df.location == country] | |
print(">> Collecting years with data") | |
dates = [date.fromisoformat(datestr) for datestr in df.date.iloc] | |
years = list(set(date.year for date in dates)) | |
print(years) | |
print(">> Plotting year data") | |
for year in years: | |
year_start = date(year, 1, 1) | |
year_end = date(year, 12, 31) | |
year_dates = [(dates[i] - year_start).days for i in range(len(dates)) if year_start <= dates[i] <= year_end] | |
year_values = [df[column].iloc[i] for i in range(len(dates)) if year_start <= dates[i] <= year_end] | |
plt.plot(year_dates, year_values, label = year) | |
plt.title(f"{country} - {column}") | |
plt.xticks( | |
[(date(2000, i, 1) - date(2000, 1, 1)).days for i in range(1, 13)], | |
[i for i in range(1, 13)], | |
) | |
plt.legend() | |
plt.show() | |
elif action == "list-columns": | |
print(">> List of Columns") | |
for column in sorted(df.columns): | |
if df.dtypes[column].type not in [np.float64, np.uint64]: | |
continue | |
print(column) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage
$ python owid-covid.py Austria
$ python owid-covid.py Austria icu_patients