Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# nb 010: Playing with Timeseries\n",
"\n",
"I need to map an irregular series of timestamps to a series of values and interpolate it with piecewise interpolation. I.e. with pairs `(9:01AM, \"O\"), (9:10AM, \"V\")`, the value `9:03AM` should map to `O`.\n",
"\n",
"This can be done by keeping a sorted list of timestamps and indexing it by a strictly increasing time-key (for $O(1)$ indexing) or indexing by an arbitrary time-key (for $O(log(n))$ indexing.)\n",
"\n",
"But Pandas might implement this already, which would probably be much faster than any solution I came up with. I need to find out how to do that. Once I do that, I need to make timelines, and then extract charging events from these.\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"index = [pd.Timestamp(\"9:00\"),\n",
" pd.Timestamp(\"9:10\"),\n",
" pd.Timestamp(\"9:12\"),\n",
" pd.Timestamp(\"9:30\"),\n",
" pd.Timestamp(\"9:31\"),\n",
" pd.Timestamp(\"9:52\")]\n",
"\n",
"data = [3, 4, 10, 7, 2, 12]\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ss = pd.Series(data=data, index=index)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2020-02-26 09:00:00 3\n",
"2020-02-26 09:10:00 4\n",
"2020-02-26 09:12:00 10\n",
"2020-02-26 09:30:00 7\n",
"2020-02-26 09:31:00 2\n",
"2020-02-26 09:52:00 12\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss[\"9:00\"]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2020-02-26 09:00:00 3.0\n",
"2020-02-26 09:05:00 NaN\n",
"2020-02-26 09:10:00 4.0\n",
"2020-02-26 09:12:00 10.0\n",
"2020-02-26 09:30:00 7.0\n",
"2020-02-26 09:31:00 2.0\n",
"2020-02-26 09:52:00 12.0\n",
"dtype: float64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Hacky Method 1 for indexing between samples\n",
"\n",
"tk = pd.Timestamp(\"9:05\")\n",
"ss[tk] = float(\"nan\")\n",
"ss = ss.sort_index()\n",
"ss"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ss = ss.interpolate(\"pad\")\n",
"ss[tk] #should be 3, not 12."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"assert ss[tk] == ss[\"9:00\"]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Better method!\n",
"idx = ss.index.get_loc(pd.Timestamp(\"9:02\"), method=\"ffill\")\n",
"ss[idx]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"tk = pd.Timestamp(\"9:51\")\n",
"idx = ss.index.get_loc(pd.Timestamp(tk), method=\"ffill\")\n",
"val = ss[idx]\n",
"\n",
"assert val == 2.0\n",
"\n",
"# Awesome!! This works great."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}