22import datetime
33from io import StringIO ,BytesIO
44import sys
5+ import warnings
56import zipfile
67
78import 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" ]
0 commit comments