rolling return now returned as TimeSeries object

This commit is contained in:
Gourav Kumar 2022-02-21 23:08:13 +05:30
parent 04176646b6
commit 9ddf70fc61
2 changed files with 62 additions and 29 deletions

View File

@ -225,7 +225,7 @@ class TimeSeries(TimeSeriesCore):
) )
rolling_returns.append((i, returns)) rolling_returns.append((i, returns))
rolling_returns.sort() rolling_returns.sort()
return rolling_returns return self.__class__(rolling_returns, self.frequency.symbol)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -22,7 +22,6 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"dfd = pd.read_csv('test_files/nav_history_daily - copy.csv')\n", "dfd = pd.read_csv('test_files/nav_history_daily - copy.csv')\n",
"\n",
"dfd = dfd[dfd['amfi_code'] == 118825].reset_index(drop=True)" "dfd = dfd[dfd['amfi_code'] == 118825].reset_index(drop=True)"
] ]
}, },
@ -58,7 +57,6 @@
], ],
"source": [ "source": [
"ts = TimeSeries([(i.date, i.nav) for i in dfd.itertuples()], frequency='D')\n", "ts = TimeSeries([(i.date, i.nav) for i in dfd.itertuples()], frequency='D')\n",
"\n",
"ts" "ts"
] ]
}, },
@ -86,18 +84,35 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 16,
"id": "4d927a61-0f90-4b47-89b7-0e0d3ab1b442", "id": "086d4377-d1b1-4e51-84c0-39dee28ef75e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"TimeSeries([(datetime.datetime(2021, 2, 15, 0, 0), 73.483),\n",
"\t (datetime.datetime(2021, 2, 16, 0, 0), 73.237),\n",
"\t (datetime.datetime(2021, 2, 17, 0, 0), 72.98)\n",
"\t ...\n",
"\t (datetime.datetime(2022, 2, 10, 0, 0), 86.5),\n",
"\t (datetime.datetime(2022, 2, 11, 0, 0), 85.226),\n",
"\t (datetime.datetime(2022, 2, 14, 0, 0), 82.53299999999999)], frequency='D')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"s = ts.dates > '2020-01-01'" "ts[ts.dates>'2021-02-14']"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 5,
"id": "f90074f8-5173-49a9-a7d6-ceac01e92431", "id": "6f1226a3-2327-435b-88e7-fd0fdcc8cc1c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -112,18 +127,49 @@
"\t (datetime.datetime(2022, 2, 14, 0, 0), 82.53299999999999)], frequency='D')" "\t (datetime.datetime(2022, 2, 14, 0, 0), 82.53299999999999)], frequency='D')"
] ]
}, },
"execution_count": 6, "execution_count": 5,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"s = ts.dates > '2020-01-01'\n",
"ts[s]" "ts[s]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 9,
"id": "e815edc9-3746-4192-814e-bd27b2771a0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(datetime.datetime(2013, 1, 2, 0, 0), 18.972),\n",
" (datetime.datetime(2013, 1, 3, 0, 0), 19.011),\n",
" (datetime.datetime(2013, 1, 4, 0, 0), 19.008),\n",
" (datetime.datetime(2013, 1, 7, 0, 0), 18.95),\n",
" (datetime.datetime(2013, 1, 8, 0, 0), 18.954),\n",
" (datetime.datetime(2013, 1, 9, 0, 0), 18.94),\n",
" (datetime.datetime(2013, 1, 10, 0, 0), 18.957),\n",
" (datetime.datetime(2013, 1, 11, 0, 0), 18.948),\n",
" (datetime.datetime(2013, 1, 14, 0, 0), 19.177),\n",
" (datetime.datetime(2013, 1, 15, 0, 0), 19.272000000000002)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ts.iloc[:10]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dc469722-c816-4b57-8d91-7a3b865f86be", "id": "dc469722-c816-4b57-8d91-7a3b865f86be",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -134,7 +180,7 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"CPU times: total: 15.6 ms\n", "CPU times: total: 15.6 ms\n",
"Wall time: 13 ms\n" "Wall time: 14 ms\n"
] ]
} }
], ],
@ -148,24 +194,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": null,
"id": "086d4377-d1b1-4e51-84c0-39dee28ef75e", "id": "e5d357b4-4fe5-4a0a-8107-0ab6828d7c41",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{ "source": []
"data": {
"text/plain": [
"list"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(rr)"
]
} }
], ],
"metadata": { "metadata": {