forked from owid/covid-19-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalta.py
90 lines (74 loc) · 3.21 KB
/
malta.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pandas as pd
from cowidev.utils.clean import clean_date_series
from cowidev.vax.utils.utils import make_monotonic
from cowidev.utils import paths
class Malta:
location: str = "Malta"
source_url: str = (
"https://github.com/COVID19-Malta/COVID19-Cases/raw/master/COVID-19%20Malta%20-%20Vaccination%20Data.csv"
)
source_url_ref: str = "https://github.com/COVID19-Malta/COVID19-Cases"
columns_rename: dict = {
"Date of Vaccination": "date",
"Total Vaccination Doses": "total_vaccinations",
"Fully vaccinated (2 of 2 or 1 of 1)": "people_fully_vaccinated",
"Received one dose (1 of 2 or 1 of 1)": "people_vaccinated",
"Total Booster doses": "total_boosters",
}
def read(self) -> pd.DataFrame:
return pd.read_csv(self.source_url)
def pipe_check_columns(self, df: pd.DataFrame) -> pd.DataFrame:
columns_wrong = set(df.columns).difference(self.columns_rename)
if columns_wrong:
raise ValueError(f"Invalid column name(s): {columns_wrong}")
return df
def pipe_rename_columns(self, df: pd.DataFrame) -> pd.DataFrame:
return df.rename(columns=self.columns_rename)
def pipe_correct_data(self, df: pd.DataFrame) -> pd.DataFrame:
df.loc[
(df.people_fully_vaccinated == 0) | df.people_fully_vaccinated.isnull(),
"people_vaccinated",
] = df.total_vaccinations
return df
def pipe_date(self, df: pd.DataFrame) -> pd.DataFrame:
return df.assign(date=clean_date_series(df.date, "%d/%m/%Y"))
def pipe_vaccine(self, df: pd.DataFrame) -> pd.DataFrame:
def _enrich_vaccine_name(date: str) -> str:
# See timeline in:
if date < "2021-02-03":
return "Pfizer/BioNTech"
if "2021-02-03" <= date < "2021-02-10":
return "Moderna, Pfizer/BioNTech"
elif "2021-02-10" <= date < "2021-05-06":
return "Moderna, Oxford/AstraZeneca, Pfizer/BioNTech"
elif "2021-05-06" <= date:
return "Johnson&Johnson, Moderna, Oxford/AstraZeneca, Pfizer/BioNTech"
return df.assign(vaccine=df.date.apply(_enrich_vaccine_name))
def pipe_metadata(self, df: pd.DataFrame) -> pd.DataFrame:
return df.assign(
location=self.location,
source_url=self.source_url_ref,
)
def pipe_exclude_data_points(self, df: pd.DataFrame) -> pd.DataFrame:
return df[
(df.people_vaccinated >= df.people_fully_vaccinated)
| (df.people_vaccinated.isna())
| (df.people_fully_vaccinated.isna())
]
def pipeline(self, df: pd.DataFrame) -> pd.DataFrame:
return (
df.pipe(self.pipe_check_columns)
.pipe(self.pipe_rename_columns)
.pipe(self.pipe_correct_data)
.pipe(self.pipe_date)
.pipe(self.pipe_metadata)
.pipe(self.pipe_vaccine)
.pipe(self.pipe_exclude_data_points)
.pipe(make_monotonic)
)
def export(self):
df = self.read().pipe(self.pipeline)
destination = paths.out_vax(self.location)
df.to_csv(destination, index=False)
def main():
Malta().export()