Using Brazilian epidemiological week definition

Brazilian epidemiological week (epiweek) is definied from Sunday to Saturday.

Wednesday is defined as the turning point for changing from one year to another and deciding wether Jan 1st is inlcuded in the first epiweek of the new year or still in the last epiweek of the previous one. That is, if the weekday of Jan 1st is between Sunday to Wednesday (included), then it is epiweek 01 of the new year. If the weekday is between Thursday-Saturday, then it falls in the last epiweek of the previous year (typically epiweek 52).

The function epiweek takes both information into account to return the epiweek relative to a given date. Input can be a string in the format YYYY-MM-DD or type datetime.datetime

See episem.py for details

Import module episem


In [2]:
from episem import episem

Example 1: 2016-10-31

Function episem can take 1 to 3 inputs:

episem(x, sep='W', out='YW')
'''
@param x: date in format string YYYY-MM-DD or as datetime.datetime
@param sep: separator character
@param out: returned info. Y returns epiyear alone, W returns only epiweek and YW returns
epiyear and epiweek separated by sep

returns str
'''

Passing string


In [5]:
d = '2010-10-01'

In [6]:
episem(d)


Out[6]:
'2010W39'

In [8]:
episem(d,out='W')


Out[8]:
'39'

Passing datetime.datetime


In [15]:
import datetime

In [16]:
datetime.datetime.strptime(d, '%Y-%m-%d')


Out[16]:
datetime.datetime(2010, 10, 1, 0, 0)

In [17]:
dt = datetime.datetime.strptime(d, '%Y-%m-%d')
episem(dt)


Out[17]:
'2010W39'

Example 2: 2016-01-01

2016-10-01 was a Friday, as can be seen by isoweekday function, which returns 1 for Monday and 7 for Sunday:


In [27]:
dt2 = datetime.datetime.strptime('2016-01-01', '%Y-%m-%d')
dt2.isoweekday()


Out[27]:
5

Therefore, according to Brazilian epiweek system, it should fall on the last epiweek of year 2105:


In [28]:
episem(dt2)


Out[28]:
'2015W52'

Example 3: 2017-01-01

2017-10-01 is a Sunday. Therefore, it should fall on the first epiweek of 2017:


In [41]:
dt3 = datetime.datetime.strptime('2017-01-01', '%Y-%m-%d')
dt3.isoweekday()


Out[41]:
7

In [42]:
episem(dt3)


Out[42]:
'2017W01'

Comparing with isocalendar

The week of the year, according to ISO calendar, is calculated slightly different from the Brazilian epiweek system. Thefore, although for some days of the year it can be the same numeral, for others the results can differ. In particular, the base year change and the introduction or not of week 53 are very sensitive. We show here the comparison between the two systems for the dates already used to highlight this issue.

The function isocalendar returns the base year, the week and the weekday for a given date.


In [52]:
print('Date: %s\nISO-calendar: %s\nBR-epiweek: %s\n' % (dt.date(), dt.isocalendar(), episem(dt)))


Date: 2010-10-01
ISO-calendar: (2010, 39, 5)
BR-epiweek: 2010W39


In [53]:
print('Date: %s\nISO-calendar: %s\nBR-epiweek: %s\n' % (dt2.date(), dt2.isocalendar(), episem(dt2)))


Date: 2016-01-01
ISO-calendar: (2015, 53, 5)
BR-epiweek: 2015W52


In [54]:
print('Date: %s\nISO-calendar: %s\nBR-epiweek: %s\n' % (dt3.date(), dt3.isocalendar(), episem(dt3)))


Date: 2017-01-01
ISO-calendar: (2016, 52, 7)
BR-epiweek: 2017W01