CoCalc Public FilesVIS / tests.sagewsOpen with one click!
Author: Hal Snyder
Views : 156
Description: Sample worksheet: display of IL COVID-19 testing data

COVID-19 Display Testing Time Series

%auto %default_mode python3
import pandas as pd import numpy as np
state = 'il' # use testing series collected by The Covid Tracking Project # https://covidtracking.com/api/ # state abbreviation must be lower case if state == 'US': url = f"https://covidtracking.com/api/us/daily.csv" else: url = f"https://covidtracking.com/api/v1/states/{state}/daily.csv" url
'https://covidtracking.com/api/v1/states/il/daily.csv'
# fetch state data from Covid Tracking Project # keep only a few columns df = pd.read_csv(url, parse_dates=['date', 'dateChecked'])[['date','positive','death','total']]
# use date as x-axis for displays so make it the index df.set_index('date', inplace=True) df.set_index(df.index.strftime('%m/%d'), inplace=True) df.sort_index(inplace=True)
df.head(2)
positive death total
date
03/04 4 NaN 155
03/05 5 NaN 197
df.tail()
positive death total
date
06/18 135639 6718.0 1284693
06/19 136470 6718.0 1312003
06/20 137104 6829.0 1337968
06/21 137762 6851.0 1361784
06/22 138224 6875.0 1380003
title = f"COVID-19 cumulative numbers\npositive tests and total tests in {state}" df[-30:].plot(y=['positive','total'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
df['positive/total'] = df.loc[:,'positive']/df.loc[:,'total']
title = f"COVID-19 positive tests / total tests in {state}" df[-30:].plot(y=['positive/total'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
# just the confirmed (positives) title = f"COVID-19 cumulative confirmed cases in {state}" df[-30:].plot(y=['positive'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
df['new_cases_per_day'] = df['positive'].diff() title = f"COVID-19 new cases reported each day in {state}" df[-30:].plot(y=['new_cases_per_day'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
title = f"COVID-19 7-day moving avg new cases in {state}" df['7d_avg_new_cases'] = df['new_cases_per_day'].rolling(7).mean() df[-30:].plot(y=['7d_avg_new_cases'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
# deaths title = f"COVID-19 deaths in {state}" df[-30:].plot(y=['death'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
df['deaths_each_day'] = df['death'].diff()
title = f"COVID-19 deaths each day in {state}" df[-30:].plot(y=['deaths_each_day'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);
title = f"COVID-19 7-day moving avg deaths in {state}" df['7d_avg_deaths'] = df['deaths_each_day'].rolling(7).mean() df[-30:].plot(y=['7d_avg_deaths'], kind="bar", figsize=[8, 5], rot=45, fontsize=7, title=title);