Skip to content

Commit a39cbca

Browse files
filtering by country and date
1 parent 8d3bb38 commit a39cbca

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

covid19dh/main.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
from io import StringIO,BytesIO
44
import sys
5+
import warnings
56
import zipfile
67

78
import pandas as pd
@@ -22,13 +23,18 @@ def covid19(country = None,
2223
level = 1,
2324
start = datetime.date(2019,1,1),
2425
end = None, # defaultly today
26+
# TODO
2527
raw = False,
2628
vintage = False,
2729
verbose = True,
2830
cache = True):
29-
# default today
30-
if not end:
31-
end = datetime.date.today()
31+
# parse arguments
32+
country = country.upper() if country is not None else None
33+
end = datetime.datetime.now() if end is None else end
34+
if isinstance(end, datetime.date):
35+
end = datetime.datetime(end.year, end.month, end.day)
36+
if isinstance(start, datetime.date):
37+
start = datetime.datetime(start.year, start.month, start.day)
3238

3339
# get url from level
3440
try:
@@ -42,9 +48,24 @@ def covid19(country = None,
4248
# parse
4349
with zipfile.ZipFile( BytesIO(response.content) ) as zz:
4450
with zz.open(filename) as fd:
45-
x = pd.read_csv( fd )
51+
df = pd.read_csv( fd )
52+
# cast columns
53+
df['date'] = df['date'].apply(lambda x: datetime.datetime.strptime(x, "%Y-%m-%d"))
4654

55+
# filter
56+
if country is not None:
57+
# elementwise comparison works, but throws warning that it will be working better in the future
58+
# no idea why, but I found solution to mute it as follows
59+
with warnings.catch_warnings():
60+
warnings.simplefilter(action='ignore', category=FutureWarning)
61+
df = df[(df['iso_alpha_3'] == country) |
62+
(df['iso_alpha_2'] == country) |
63+
(df['iso_numeric'] == country) ]
64+
if start is not None:
65+
df = df[df['date'] >= start]
66+
if end is not None:
67+
df = df[df['date'] <= end]
4768

48-
return x
69+
return df
4970

5071
__all__ = ["covid19"]

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
setuptools.setup(
77
name = 'covid19dh',
8-
version = '0.0.1',
8+
version = '0.0.2',
99
author = 'Covid-19 Data Hub',
10-
author_email = 'emanuele.guidotti@unine.ch',
10+
author_email = 'martinbenes1996@gmail.com',
1111
description = 'Unified data hub for a better understanding of COVID-19 https://covid19datahub.io',
1212
long_description = long_description,
1313
long_description_content_type="text/markdown",
1414
packages=setuptools.find_packages(),
1515
license='GPL',
1616
url = 'https://www.covid19datahub.io/',
17-
download_url = 'https://github.com/covid19datahub/Python/archive/0.0.1.tar.gz',
17+
download_url = 'https://github.com/covid19datahub/Python/archive/0.0.2.tar.gz',
1818
keywords = ['2019-nCov', 'coronavirus', 'covid-19', 'covid-data', 'covid19-data'],
1919
install_requires=[],
2020
package_dir={'': '.'},

0 commit comments

Comments
 (0)