{ "metadata": { "name": "", "signature": "sha256:994e18f56a6d5d829fcd7de4a9cd2260c16c431f9019d3cb1e82bb9384a5745d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert A Variable To A Time Variable In Pandas\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Import Preliminaries\n", "import pandas as pd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a dataset with the index being a set of names\n", "raw_data = {'date': ['2014-06-01T01:21:38.004053', '2014-06-02T01:21:38.004053', '2014-06-03T01:21:38.004053'],\n", " 'score': [25, 94, 57]}\n", "df = pd.DataFrame(raw_data, columns = ['date', 'score'])\n", "df" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datescore
0 2014-06-01T01:21:38.004053 25
1 2014-06-02T01:21:38.004053 94
2 2014-06-03T01:21:38.004053 57
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ " date score\n", "0 2014-06-01T01:21:38.004053 25\n", "1 2014-06-02T01:21:38.004053 94\n", "2 2014-06-03T01:21:38.004053 57" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "# Transpose the dataset, so that the index (in this case the names) are columns\n", "df[\"date\"] = pd.to_datetime(df[\"date\"])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "df = df.set_index(df[\"date\"])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "df" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datescore
date
2014-06-01 01:21:38.0040532014-06-01 01:21:38.004053 25
2014-06-02 01:21:38.0040532014-06-02 01:21:38.004053 94
2014-06-03 01:21:38.0040532014-06-03 01:21:38.004053 57
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ " date score\n", "date \n", "2014-06-01 01:21:38.004053 2014-06-01 01:21:38.004053 25\n", "2014-06-02 01:21:38.004053 2014-06-02 01:21:38.004053 94\n", "2014-06-03 01:21:38.004053 2014-06-03 01:21:38.004053 57" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 } ], "metadata": {} } ] }