convertir en séries pandas

parent d79926c5
...@@ -3194,12 +3194,48 @@ ...@@ -3194,12 +3194,48 @@
"sorted_data" "sorted_data"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Choisir le 1er spetembre comme début de chaque période anuelle"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 29,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
"first_september_week = [pd.Period(pd.Timestamp(y, 9, 1), 'w')\n",
" for y in range(sorted_data.index[0].year,\n",
" sorted_data.index[-1].year)]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"year = []\n",
"yearly_incidence = []\n",
"for week1, week2 in zip(first_september_week[:-1],\n",
" first_september_week[1:]):\n",
" one_year = sorted_data['inc'][week1:week2-1]\n",
" yearly_incidence.append(one_year.sum())\n",
" year.append(week2.year)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"# Conertir en séries pandas\n",
"yearly_incidence = pd.Series(index = year, data = yearly_incidence)"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
...@@ -3224,37 +3260,25 @@ ...@@ -3224,37 +3260,25 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 26,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "NameError", "ename": "NameError",
"evalue": "name 'sorted_data' is not defined", "evalue": "name 'first_september_week' is not defined",
"output_type": "error", "output_type": "error",
"traceback": [ "traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-e4ec19bfbf99>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m first_spetember_week = [pd.Period(pd.Timestamp(y, 9, 1), 'w')\n\u001b[0;32m----> 4\u001b[0;31m for y in range(sorted_data.index[0].year,\n\u001b[0m\u001b[1;32m 5\u001b[0m sorted_data.index[-1].year)]\n", "\u001b[0;32m<ipython-input-26-7e6b8a051dc5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0myear\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0myearly_incidence\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m for week1, week2 in zip(first_september_week[:-1],\n\u001b[0m\u001b[1;32m 11\u001b[0m first_september_week[1:]):\n\u001b[1;32m 12\u001b[0m \u001b[0mone_year\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msorted_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'inc'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mweek1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mweek2\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'sorted_data' is not defined" "\u001b[0;31mNameError\u001b[0m: name 'first_september_week' is not defined"
] ]
} }
], ],
"source": [ "source": [
"# Choisir le 1er spetembre comme début de chaque période anuelle\n",
"\n",
"first_spetember_week = [pd.Period(pd.Timestamp(y, 9, 1), 'w')\n",
" for y in range(sorted_data.index[0].year,\n",
" sorted_data.index[-1].year)]\n",
"\n", "\n",
"\n", "\n",
"year = []\n", "\n"
"yearly_incidence = []\n",
"for week1, week2 in zip(first_september_week[:-1],\n",
" 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)"
] ]
}, },
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment