Example usage
To use covid_tracker
in a project:
import covid_tracker
from covid_tracker.get_covid_data import get_covid_data
from covid_tracker.plot_geographical import plot_geographical
from covid_tracker.plot_time_series import plot_ts
from covid_tracker.calculate_stat_summary import calculate_stat_summary
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [1], in <module>
1 import covid_tracker
----> 2 from covid_tracker.get_covid_data import get_covid_data
3 from covid_tracker.plot_geographical import plot_geographical
4 from covid_tracker.plot_time_series import plot_ts
ModuleNotFoundError: No module named 'covid_tracker.get_covid_data'
Retrieve the Data
We first retrieve the data using the get_covid_data
function, and in this case we are using data_type = ‘active’ and all locations and dates by default.
covid_df = get_covid_data('active')
covid_df
active_cases | active_cases_change | cumulative_cases | cumulative_deaths | cumulative_recovered | date_active | province | |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 25-01-2020 | Alberta |
1 | 0 | 0 | 0 | 0 | 0 | 26-01-2020 | Alberta |
2 | 0 | 0 | 0 | 0 | 0 | 27-01-2020 | Alberta |
3 | 0 | 0 | 0 | 0 | 0 | 28-01-2020 | Alberta |
4 | 0 | 0 | 0 | 0 | 0 | 29-01-2020 | Alberta |
... | ... | ... | ... | ... | ... | ... | ... |
10229 | 213 | -16 | 2820 | 15 | 2592 | 20-01-2022 | Yukon |
10230 | 196 | -17 | 2849 | 16 | 2637 | 21-01-2022 | Yukon |
10231 | 196 | 0 | 2849 | 16 | 2637 | 22-01-2022 | Yukon |
10232 | 196 | 0 | 2849 | 16 | 2637 | 23-01-2022 | Yukon |
10233 | 194 | -2 | 2959 | 16 | 2749 | 24-01-2022 | Yukon |
10234 rows × 7 columns
Plot Map
We can then plot a chloropleth map showing the cumulative number of deaths in each province across Canada.
plot_geographical(covid_df,'cumulative_deaths')
<Figure size 432x288 with 0 Axes>
Plot Time Series
We can also plot a time series graph showing the number of active cases over time.
plot_ts(covid_df, "active_cases")
---------------------------------------------------------------------------
MaxRowsError Traceback (most recent call last)
File ~\miniconda3\envs\covtrack\lib\site-packages\altair\vegalite\v4\api.py:2020, in Chart.to_dict(self, *args, **kwargs)
2018 copy.data = core.InlineData(values=[{}])
2019 return super(Chart, copy).to_dict(*args, **kwargs)
-> 2020 return super().to_dict(*args, **kwargs)
File ~\miniconda3\envs\covtrack\lib\site-packages\altair\vegalite\v4\api.py:374, in TopLevelMixin.to_dict(self, *args, **kwargs)
372 copy = self.copy(deep=False)
373 original_data = getattr(copy, "data", Undefined)
--> 374 copy.data = _prepare_data(original_data, context)
376 if original_data is not Undefined:
377 context["data"] = original_data
File ~\miniconda3\envs\covtrack\lib\site-packages\altair\vegalite\v4\api.py:89, in _prepare_data(data, context)
87 # convert dataframes or objects with __geo_interface__ to dict
88 if isinstance(data, pd.DataFrame) or hasattr(data, "__geo_interface__"):
---> 89 data = _pipe(data, data_transformers.get())
91 # convert string input to a URLData
92 if isinstance(data, str):
File ~\miniconda3\envs\covtrack\lib\site-packages\toolz\functoolz.py:630, in pipe(data, *funcs)
610 """ Pipe a value through a sequence of functions
611
612 I.e. ``pipe(data, f, g, h)`` is equivalent to ``h(g(f(data)))``
(...)
627 thread_last
628 """
629 for func in funcs:
--> 630 data = func(data)
631 return data
File ~\miniconda3\envs\covtrack\lib\site-packages\toolz\functoolz.py:306, in curry.__call__(self, *args, **kwargs)
304 def __call__(self, *args, **kwargs):
305 try:
--> 306 return self._partial(*args, **kwargs)
307 except TypeError as exc:
308 if self._should_curry(args, kwargs, exc):
File ~\miniconda3\envs\covtrack\lib\site-packages\altair\vegalite\data.py:19, in default_data_transformer(data, max_rows)
17 @curried.curry
18 def default_data_transformer(data, max_rows=5000):
---> 19 return curried.pipe(data, limit_rows(max_rows=max_rows), to_values)
File ~\miniconda3\envs\covtrack\lib\site-packages\toolz\functoolz.py:630, in pipe(data, *funcs)
610 """ Pipe a value through a sequence of functions
611
612 I.e. ``pipe(data, f, g, h)`` is equivalent to ``h(g(f(data)))``
(...)
627 thread_last
628 """
629 for func in funcs:
--> 630 data = func(data)
631 return data
File ~\miniconda3\envs\covtrack\lib\site-packages\toolz\functoolz.py:306, in curry.__call__(self, *args, **kwargs)
304 def __call__(self, *args, **kwargs):
305 try:
--> 306 return self._partial(*args, **kwargs)
307 except TypeError as exc:
308 if self._should_curry(args, kwargs, exc):
File ~\miniconda3\envs\covtrack\lib\site-packages\altair\utils\data.py:80, in limit_rows(data, max_rows)
78 return data
79 if max_rows is not None and len(values) > max_rows:
---> 80 raise MaxRowsError(
81 "The number of rows in your dataset is greater "
82 "than the maximum allowed ({}). "
83 "For information on how to plot larger datasets "
84 "in Altair, see the documentation".format(max_rows)
85 )
86 return data
MaxRowsError: The number of rows in your dataset is greater than the maximum allowed (5000). For information on how to plot larger datasets in Altair, see the documentation
alt.Chart(...)
Calculate Summary
Lastly, we can create a summary of our covid tracking information.
calculate_stat_summary(covid_df, 'active')
date_active | province | active_cases | active_cases_change | cumulative_cases | cumulative_deaths | cumulative_recovered | active_cases_min | active_cases_max | active_cases_mean | |
---|---|---|---|---|---|---|---|---|---|---|
718 | 2022-12-01 | Alberta | 61229 | 2616 | 424322 | 3367 | 359726 | 0 | 72368 | 8045 |
1449 | 2022-12-01 | BC | 39544 | 464 | 288692 | 2455 | 246693 | 0 | 40269 | 4622 |
2180 | 2022-12-01 | Manitoba | 35048 | 1431 | 105406 | 1429 | 68929 | 0 | 41708 | 2535 |
3642 | 2022-12-01 | NL | 6346 | 213 | 10827 | 23 | 4458 | 0 | 6346 | 178 |
5835 | 2022-12-01 | NWT | 1145 | 120 | 3467 | 12 | 2310 | 0 | 1306 | 58 |
2911 | 2022-12-01 | New Brunswick | 7060 | -289 | 21249 | 174 | 14015 | 0 | 7935 | 367 |
4373 | 2022-12-01 | Nova Scotia | 6867 | -545 | 28194 | 117 | 21210 | 0 | 8597 | 418 |
5104 | 2022-12-01 | Nunavut | 186 | -8 | 1036 | 4 | 846 | 0 | 285 | 15 |
6566 | 2022-12-01 | Ontario | 116623 | -3454 | 921332 | 10406 | 794303 | 0 | 126695 | 15272 |
7297 | 2022-12-01 | PEI | 1922 | 228 | 3427 | 0 | 1505 | 0 | 2514 | 71 |
8028 | 2022-12-01 | Quebec | 100448 | -5802 | 758576 | 12028 | 646100 | 0 | 123058 | 12467 |
8759 | 2022-12-01 | Repatriated | 0 | 0 | 13 | 0 | 13 | 0 | 13 | 0 |
9490 | 2022-12-01 | Saskatchewan | 8707 | 477 | 95174 | 960 | 85507 | 0 | 13291 | 1551 |
10221 | 2022-12-01 | Yukon | 429 | 138 | 2515 | 15 | 2071 | 0 | 429 | 28 |