diff --git a/module3/exo2/exercice.ipynb b/module3/exo2/exercice.ipynb index 0bbbe371b01e359e381e43239412d77bf53fb1fb..710f72acd88a04ba65349bef4968594139b7214e 100644 --- a/module3/exo2/exercice.ipynb +++ b/module3/exo2/exercice.ipynb @@ -1,5 +1,90 @@ { - "cells": [], + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import isoweek" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def convert_week(year_and_week_int):\n", + " year_and_week_str = str(year_and_week_int)\n", + " year = int(year_and_week_str[:4])\n", + " week = int(year_and_week_str[4:])\n", + " w = isoweek.Week(year, week)\n", + " return pd.Period(w.day(0), 'W')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2020 221186\n", + "2002 516689\n", + "2018 542312\n", + "2017 551041\n", + "1996 564901\n", + "2019 584066\n", + "2015 604382\n", + "2000 617597\n", + "2001 619041\n", + "2012 624573\n", + "2005 628464\n", + "2006 632833\n", + "2011 642368\n", + "1993 643387\n", + "1995 652478\n", + "1994 661409\n", + "1998 677775\n", + "1997 683434\n", + "2014 685769\n", + "2013 698332\n", + "2007 717352\n", + "2008 749478\n", + "1999 756456\n", + "2003 758363\n", + "2004 777388\n", + "2016 782114\n", + "2010 829911\n", + "1992 832939\n", + "2009 842373\n", + "dtype: int64" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = pd.read_csv(\"https://www.sentiweb.fr/datasets/incidence-PAY-7.csv\", skiprows=1).dropna().copy()\n", + "data['period'] = [convert_week(yw) for yw in data['week']]\n", + "sorted_data = data.set_index('period').sort_index()\n", + "first_september_week = [pd.Period(pd.Timestamp(y, 9, 1), 'W') for y in range(1991, sorted_data.index[-1].year)]\n", + "year = []\n", + "yearly_incidence = []\n", + "for week1, week2 in zip(first_september_week[:-1], first_september_week[1:]):\n", + " one_year = sorted_data['inc'][week1:week2-1]\n", + " assert abs(len(one_year)-52) < 2\n", + " yearly_incidence.append(one_year.sum())\n", + " year.append(week2.year)\n", + "pd.Series(data=yearly_incidence, index=year).sort_values()" + ] + } + ], "metadata": { "kernelspec": { "display_name": "Python 3", @@ -16,10 +101,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 } -