-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkNYT.py
68 lines (53 loc) · 1.99 KB
/
WorkNYT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 10 12:14:05 2021
@author: matt_
"""
import covid
import pandas as pd
import numpy as np
import plotly.express as px
import datetime
import os
#%% Load NYT data by state
# If I download from my fork
# states_csv = '..\\covid-19-data-nyt\\us-states.csv'
# states_df = pd.read_csv(states_csv)
# Just download right from the original github repository
states_csv_url = 'https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv'
states_df = pd.read_csv(states_csv_url)
state_selection = ['Illinois', 'Michigan', 'Minnesota', 'Wisconsin']
population = {'Illinois' : 12.7e6,
'Michigan' : 9.99e6,
'Minnesota': 5.64e6,
'Wisconsin': 5.82e6}
selection = states_df[states_df.state.isin(state_selection)].copy()
selection.loc[:,'date'] = pd.to_datetime(selection.date)
selection = selection.rename(columns={'date':'Date', 'state':'State', 'cases':'Cases', 'deaths':'Deaths'})
selection['Cases per million'] = selection.apply(lambda row: row.Cases / population[row.State] * 1e6, axis=1)
# pivot so can do date-based processing
# 7-day average of daily numbers is equal to 7-day diff of cumulative numbers
p = selection.pivot(index='Date', columns='State', values='Cases per million')
p = p.diff(periods=7)/7
# Melt to the right format for px.line
plotdata = p.melt(value_name='Cases per million', ignore_index=False).reset_index()
# Limit dates
plotdata = plotdata[plotdata.Date > datetime.datetime(2020, 1, 15)]
# plotdata = plotdata[plotdata.Date < datetime.datetime(2021, 4, 10)]
#%% Plot
fig = px.line(
plotdata,
x='Date',
y='Cases per million',
color='State',
color_discrete_map={'Illinois':'#13294B', 'Michigan':'#FFCB05', 'Minnesota':'#862334', 'Wisconsin':'#C4012F'},
title='Covid cases for IL/MI/MN/WI<br>(7-day avg., per pop.)'
)
pngfile = 'docs\\assets\\Cases-Midwest-States.png'
fig.write_image(
pngfile,
width=700,
height=450,
engine='kaleido',
)
os.startfile(pngfile)