When you type the shortcut =C-c C-e h o=, this document will be
exported as HTML. All the code in it will be re-executed, and the
results will be retrieved and included into the exported document. If
you do not want to re-execute all code each time, you can delete the #
and the space before ~#+PROPERTY:~ in the header of this document.
import datetime
from urllib.request import urlretrieve
import os
Like we showed in the video, Python code is included as follows (and
is exxecuted by typing ~C-c C-c~):
if not os.path.exists(data_file):
urlretrieve(data_url, data_file)
#+begin_src python :results output :exports both
print("Hello world!")
#+end_src
print(f'Data is retrieved at {datetime.datetime.utcnow()} UTC')
#+END_SRC
#+RESULTS:
: Hello world!
:
: Data is retrieved at 2020-09-16 22:07:31.650075 UTC
And now the same but in an Python session. With a session, Python's
state, i.e. the values of all the variables, remains persistent from
one code block to the next. The code is still executed using ~C-c
C-c~.
Now we extract the interesting part of the data. from the format of the file The week is column 0, incidence is column 4. We took Monday as the first day of the week so '%W' code in python/pandas.
#+begin_src python :results output :session :exports both
import numpy
x=numpy.linspace(-15,15)
print(x)
#+end_src
#+BEGIN_SRC python :session *python* :results outputs :export both
import pandas as pd
data = pd.read_csv(data_file, skiprows=2, header=None)
data = data.loc[:, [0, 2]].rename(columns={0:'Datetime', 2:'Incidence'})
Additionally, the data starts at the beginning of 1991 and ends at the beginning of the 2020. The monthly evolution is not clear from the long timeseries. Plotting a shorter version.