Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bllchmbrs/641c3681a5f9aec1afbc to your computer and use it in GitHub Desktop.
Save bllchmbrs/641c3681a5f9aec1afbc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"worksheets": [
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"input": "import pandas as pd\nimport numpy as np",
"prompt_number": 1,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "df = pd.DataFrame(np.random.randn(50))",
"prompt_number": 8,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "df.shape",
"prompt_number": 14,
"outputs": [
{
"text": "(50, 1)",
"output_type": "pyout",
"metadata": {},
"prompt_number": 14
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now the question becomes, we have 50 observations and we know that those occur every 10 seconds - how do we create that range?\n\nFirstly, we need to generate the entire range from start to end.\n\nWe know that it occurs 50obs. \\* 10 seconds = 500 seconds"
},
{
"metadata": {},
"cell_type": "code",
"input": "pd.date_range(start='5-30-2015 00:00:00',periods=df.shape[0] * 10,freq='S')",
"prompt_number": 70,
"outputs": [
{
"text": "<class 'pandas.tseries.index.DatetimeIndex'>\n[2015-05-30 00:00:00, ..., 2015-05-30 00:08:19]\nLength: 500, Freq: S, Timezone: None",
"output_type": "pyout",
"metadata": {},
"prompt_number": 70
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Set those equal"
},
{
"metadata": {},
"cell_type": "code",
"input": "ind = pd.date_range(start='5-30-2015 00:00:00',periods=df.shape[0] * 10,freq='S') # adding one to get that last second coverage",
"prompt_number": 97,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now we need to filter to all those values that are divisible by our number, now it's occurring every 10 seconds, so essentially we need to get every date time that ends in a 0."
},
{
"metadata": {},
"cell_type": "code",
"input": "ind_df = pd.Series(ind)",
"prompt_number": 106,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "filt = ind_df.map(lambda x: x.second in [x for x in range(0,60,10)])",
"prompt_number": 107,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "ind_df[filt].head(8)",
"prompt_number": 108,
"outputs": [
{
"text": "0 2015-05-30 00:00:00\n10 2015-05-30 00:00:10\n20 2015-05-30 00:00:20\n30 2015-05-30 00:00:30\n40 2015-05-30 00:00:40\n50 2015-05-30 00:00:50\n60 2015-05-30 00:01:00\n70 2015-05-30 00:01:10\ndtype: datetime64[ns]",
"output_type": "pyout",
"metadata": {},
"prompt_number": 108
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "df.index = ind_df[filt]",
"prompt_number": 109,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "df['2015-05-30']",
"prompt_number": 110,
"outputs": [
{
"text": " 0\n2015-05-30 00:00:00 -2.541960\n2015-05-30 00:00:10 1.418795\n2015-05-30 00:00:20 0.424278\n2015-05-30 00:00:30 -0.431099\n2015-05-30 00:00:40 -0.793842\n2015-05-30 00:00:50 0.868189\n2015-05-30 00:01:00 0.948918\n2015-05-30 00:01:10 0.140921\n2015-05-30 00:01:20 -1.827831\n2015-05-30 00:01:30 -1.934219\n2015-05-30 00:01:40 -0.545092\n2015-05-30 00:01:50 -0.554454\n2015-05-30 00:02:00 -0.387286\n2015-05-30 00:02:10 -2.982757\n2015-05-30 00:02:20 0.501777\n2015-05-30 00:02:30 0.552225\n2015-05-30 00:02:40 -0.659359\n2015-05-30 00:02:50 0.105699\n2015-05-30 00:03:00 -0.620858\n2015-05-30 00:03:10 0.376805\n2015-05-30 00:03:20 0.799189\n2015-05-30 00:03:30 -1.500560\n2015-05-30 00:03:40 0.454522\n2015-05-30 00:03:50 1.435372\n2015-05-30 00:04:00 -0.713203\n2015-05-30 00:04:10 0.530396\n2015-05-30 00:04:20 -1.289187\n2015-05-30 00:04:30 1.031124\n2015-05-30 00:04:40 0.664521\n2015-05-30 00:04:50 -0.364961\n2015-05-30 00:05:00 -0.162748\n2015-05-30 00:05:10 -0.258509\n2015-05-30 00:05:20 0.172308\n2015-05-30 00:05:30 0.745513\n2015-05-30 00:05:40 -0.098311\n2015-05-30 00:05:50 -1.599429\n2015-05-30 00:06:00 1.869689\n2015-05-30 00:06:10 -0.330046\n2015-05-30 00:06:20 1.295800\n2015-05-30 00:06:30 1.866889\n2015-05-30 00:06:40 -0.486605\n2015-05-30 00:06:50 2.256074\n2015-05-30 00:07:00 -1.655385\n2015-05-30 00:07:10 1.087934\n2015-05-30 00:07:20 0.107526\n2015-05-30 00:07:30 -1.509951\n2015-05-30 00:07:40 -0.798577\n2015-05-30 00:07:50 1.921219\n2015-05-30 00:08:00 -1.495371\n2015-05-30 00:08:10 0.647703",
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>0</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2015-05-30 00:00:00</th>\n <td>-2.541960</td>\n </tr>\n <tr>\n <th>2015-05-30 00:00:10</th>\n <td> 1.418795</td>\n </tr>\n <tr>\n <th>2015-05-30 00:00:20</th>\n <td> 0.424278</td>\n </tr>\n <tr>\n <th>2015-05-30 00:00:30</th>\n <td>-0.431099</td>\n </tr>\n <tr>\n <th>2015-05-30 00:00:40</th>\n <td>-0.793842</td>\n </tr>\n <tr>\n <th>2015-05-30 00:00:50</th>\n <td> 0.868189</td>\n </tr>\n <tr>\n <th>2015-05-30 00:01:00</th>\n <td> 0.948918</td>\n </tr>\n <tr>\n <th>2015-05-30 00:01:10</th>\n <td> 0.140921</td>\n </tr>\n <tr>\n <th>2015-05-30 00:01:20</th>\n <td>-1.827831</td>\n </tr>\n <tr>\n <th>2015-05-30 00:01:30</th>\n <td>-1.934219</td>\n </tr>\n <tr>\n <th>2015-05-30 00:01:40</th>\n <td>-0.545092</td>\n </tr>\n <tr>\n <th>2015-05-30 00:01:50</th>\n <td>-0.554454</td>\n </tr>\n <tr>\n <th>2015-05-30 00:02:00</th>\n <td>-0.387286</td>\n </tr>\n <tr>\n <th>2015-05-30 00:02:10</th>\n <td>-2.982757</td>\n </tr>\n <tr>\n <th>2015-05-30 00:02:20</th>\n <td> 0.501777</td>\n </tr>\n <tr>\n <th>2015-05-30 00:02:30</th>\n <td> 0.552225</td>\n </tr>\n <tr>\n <th>2015-05-30 00:02:40</th>\n <td>-0.659359</td>\n </tr>\n <tr>\n <th>2015-05-30 00:02:50</th>\n <td> 0.105699</td>\n </tr>\n <tr>\n <th>2015-05-30 00:03:00</th>\n <td>-0.620858</td>\n </tr>\n <tr>\n <th>2015-05-30 00:03:10</th>\n <td> 0.376805</td>\n </tr>\n <tr>\n <th>2015-05-30 00:03:20</th>\n <td> 0.799189</td>\n </tr>\n <tr>\n <th>2015-05-30 00:03:30</th>\n <td>-1.500560</td>\n </tr>\n <tr>\n <th>2015-05-30 00:03:40</th>\n <td> 0.454522</td>\n </tr>\n <tr>\n <th>2015-05-30 00:03:50</th>\n <td> 1.435372</td>\n </tr>\n <tr>\n <th>2015-05-30 00:04:00</th>\n <td>-0.713203</td>\n </tr>\n <tr>\n <th>2015-05-30 00:04:10</th>\n <td> 0.530396</td>\n </tr>\n <tr>\n <th>2015-05-30 00:04:20</th>\n <td>-1.289187</td>\n </tr>\n <tr>\n <th>2015-05-30 00:04:30</th>\n <td> 1.031124</td>\n </tr>\n <tr>\n <th>2015-05-30 00:04:40</th>\n <td> 0.664521</td>\n </tr>\n <tr>\n <th>2015-05-30 00:04:50</th>\n <td>-0.364961</td>\n </tr>\n <tr>\n <th>2015-05-30 00:05:00</th>\n <td>-0.162748</td>\n </tr>\n <tr>\n <th>2015-05-30 00:05:10</th>\n <td>-0.258509</td>\n </tr>\n <tr>\n <th>2015-05-30 00:05:20</th>\n <td> 0.172308</td>\n </tr>\n <tr>\n <th>2015-05-30 00:05:30</th>\n <td> 0.745513</td>\n </tr>\n <tr>\n <th>2015-05-30 00:05:40</th>\n <td>-0.098311</td>\n </tr>\n <tr>\n <th>2015-05-30 00:05:50</th>\n <td>-1.599429</td>\n </tr>\n <tr>\n <th>2015-05-30 00:06:00</th>\n <td> 1.869689</td>\n </tr>\n <tr>\n <th>2015-05-30 00:06:10</th>\n <td>-0.330046</td>\n </tr>\n <tr>\n <th>2015-05-30 00:06:20</th>\n <td> 1.295800</td>\n </tr>\n <tr>\n <th>2015-05-30 00:06:30</th>\n <td> 1.866889</td>\n </tr>\n <tr>\n <th>2015-05-30 00:06:40</th>\n <td>-0.486605</td>\n </tr>\n <tr>\n <th>2015-05-30 00:06:50</th>\n <td> 2.256074</td>\n </tr>\n <tr>\n <th>2015-05-30 00:07:00</th>\n <td>-1.655385</td>\n </tr>\n <tr>\n <th>2015-05-30 00:07:10</th>\n <td> 1.087934</td>\n </tr>\n <tr>\n <th>2015-05-30 00:07:20</th>\n <td> 0.107526</td>\n </tr>\n <tr>\n <th>2015-05-30 00:07:30</th>\n <td>-1.509951</td>\n </tr>\n <tr>\n <th>2015-05-30 00:07:40</th>\n <td>-0.798577</td>\n </tr>\n <tr>\n <th>2015-05-30 00:07:50</th>\n <td> 1.921219</td>\n </tr>\n <tr>\n <th>2015-05-30 00:08:00</th>\n <td>-1.495371</td>\n </tr>\n <tr>\n <th>2015-05-30 00:08:10</th>\n <td> 0.647703</td>\n </tr>\n </tbody>\n</table>\n</div>",
"output_type": "pyout",
"metadata": {},
"prompt_number": 110
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
}
],
"metadata": {}
}
],
"metadata": {
"name": "",
"signature": "sha256:08cb958eb7f632047bf4a524702b1e52c5fcc8fd2517ebc38c2e474bea44ee09"
},
"nbformat": 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment