Skip to content

Commit 2342582

Browse files
committed
added a conversion to time snippit
1 parent 79acf6b commit 2342582

4 files changed

+378
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:994e18f56a6d5d829fcd7de4a9cd2260c16c431f9019d3cb1e82bb9384a5745d"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Convert A Variable To A Time Variable In Pandas\n",
16+
"\n",
17+
"- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n",
18+
"- **Date:** -\n",
19+
"- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n",
20+
"- **Note:**"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"collapsed": false,
26+
"input": [
27+
"# Import Preliminaries\n",
28+
"import pandas as pd"
29+
],
30+
"language": "python",
31+
"metadata": {},
32+
"outputs": [],
33+
"prompt_number": 11
34+
},
35+
{
36+
"cell_type": "code",
37+
"collapsed": false,
38+
"input": [
39+
"# Create a dataset with the index being a set of names\n",
40+
"raw_data = {'date': ['2014-06-01T01:21:38.004053', '2014-06-02T01:21:38.004053', '2014-06-03T01:21:38.004053'],\n",
41+
" 'score': [25, 94, 57]}\n",
42+
"df = pd.DataFrame(raw_data, columns = ['date', 'score'])\n",
43+
"df"
44+
],
45+
"language": "python",
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"html": [
50+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
51+
"<table border=\"1\" class=\"dataframe\">\n",
52+
" <thead>\n",
53+
" <tr style=\"text-align: right;\">\n",
54+
" <th></th>\n",
55+
" <th>date</th>\n",
56+
" <th>score</th>\n",
57+
" </tr>\n",
58+
" </thead>\n",
59+
" <tbody>\n",
60+
" <tr>\n",
61+
" <th>0</th>\n",
62+
" <td> 2014-06-01T01:21:38.004053</td>\n",
63+
" <td> 25</td>\n",
64+
" </tr>\n",
65+
" <tr>\n",
66+
" <th>1</th>\n",
67+
" <td> 2014-06-02T01:21:38.004053</td>\n",
68+
" <td> 94</td>\n",
69+
" </tr>\n",
70+
" <tr>\n",
71+
" <th>2</th>\n",
72+
" <td> 2014-06-03T01:21:38.004053</td>\n",
73+
" <td> 57</td>\n",
74+
" </tr>\n",
75+
" </tbody>\n",
76+
"</table>\n",
77+
"</div>"
78+
],
79+
"metadata": {},
80+
"output_type": "pyout",
81+
"prompt_number": 12,
82+
"text": [
83+
" date score\n",
84+
"0 2014-06-01T01:21:38.004053 25\n",
85+
"1 2014-06-02T01:21:38.004053 94\n",
86+
"2 2014-06-03T01:21:38.004053 57"
87+
]
88+
}
89+
],
90+
"prompt_number": 12
91+
},
92+
{
93+
"cell_type": "code",
94+
"collapsed": false,
95+
"input": [
96+
"# Transpose the dataset, so that the index (in this case the names) are columns\n",
97+
"df[\"date\"] = pd.to_datetime(df[\"date\"])"
98+
],
99+
"language": "python",
100+
"metadata": {},
101+
"outputs": [],
102+
"prompt_number": 13
103+
},
104+
{
105+
"cell_type": "code",
106+
"collapsed": false,
107+
"input": [
108+
"df = df.set_index(df[\"date\"])"
109+
],
110+
"language": "python",
111+
"metadata": {},
112+
"outputs": [],
113+
"prompt_number": 14
114+
},
115+
{
116+
"cell_type": "code",
117+
"collapsed": false,
118+
"input": [
119+
"df"
120+
],
121+
"language": "python",
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"html": [
126+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
127+
"<table border=\"1\" class=\"dataframe\">\n",
128+
" <thead>\n",
129+
" <tr style=\"text-align: right;\">\n",
130+
" <th></th>\n",
131+
" <th>date</th>\n",
132+
" <th>score</th>\n",
133+
" </tr>\n",
134+
" <tr>\n",
135+
" <th>date</th>\n",
136+
" <th></th>\n",
137+
" <th></th>\n",
138+
" </tr>\n",
139+
" </thead>\n",
140+
" <tbody>\n",
141+
" <tr>\n",
142+
" <th>2014-06-01 01:21:38.004053</th>\n",
143+
" <td>2014-06-01 01:21:38.004053</td>\n",
144+
" <td> 25</td>\n",
145+
" </tr>\n",
146+
" <tr>\n",
147+
" <th>2014-06-02 01:21:38.004053</th>\n",
148+
" <td>2014-06-02 01:21:38.004053</td>\n",
149+
" <td> 94</td>\n",
150+
" </tr>\n",
151+
" <tr>\n",
152+
" <th>2014-06-03 01:21:38.004053</th>\n",
153+
" <td>2014-06-03 01:21:38.004053</td>\n",
154+
" <td> 57</td>\n",
155+
" </tr>\n",
156+
" </tbody>\n",
157+
"</table>\n",
158+
"</div>"
159+
],
160+
"metadata": {},
161+
"output_type": "pyout",
162+
"prompt_number": 15,
163+
"text": [
164+
" date score\n",
165+
"date \n",
166+
"2014-06-01 01:21:38.004053 2014-06-01 01:21:38.004053 25\n",
167+
"2014-06-02 01:21:38.004053 2014-06-02 01:21:38.004053 94\n",
168+
"2014-06-03 01:21:38.004053 2014-06-03 01:21:38.004053 57"
169+
]
170+
}
171+
],
172+
"prompt_number": 15
173+
},
174+
{
175+
"cell_type": "code",
176+
"collapsed": false,
177+
"input": [],
178+
"language": "python",
179+
"metadata": {},
180+
"outputs": [],
181+
"prompt_number": 15
182+
}
183+
],
184+
"metadata": {}
185+
}
186+
]
187+
}

.ipynb_checkpoints/pandas_transpose_dataframe-checkpoint.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:6389af7ff4131dae511ff28d2fbe0a6f54eb6ae62812d2914276cced9f7675b1"
4+
"signature": "sha256:f95cbe0c06a0f42dbf56fc216690f98a24781c36fd988c3bf5641e7280d062af"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -12,7 +12,7 @@
1212
"cell_type": "markdown",
1313
"metadata": {},
1414
"source": [
15-
"# Transpose A Dataframe in Pandas\n",
15+
"# Transpose A Dataframe In Pandas\n",
1616
"\n",
1717
"- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n",
1818
"- **Date:** -\n",

pandas_convert_to_datetime.ipynb

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:994e18f56a6d5d829fcd7de4a9cd2260c16c431f9019d3cb1e82bb9384a5745d"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Convert A Variable To A Time Variable In Pandas\n",
16+
"\n",
17+
"- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n",
18+
"- **Date:** -\n",
19+
"- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n",
20+
"- **Note:**"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"collapsed": false,
26+
"input": [
27+
"# Import Preliminaries\n",
28+
"import pandas as pd"
29+
],
30+
"language": "python",
31+
"metadata": {},
32+
"outputs": [],
33+
"prompt_number": 11
34+
},
35+
{
36+
"cell_type": "code",
37+
"collapsed": false,
38+
"input": [
39+
"# Create a dataset with the index being a set of names\n",
40+
"raw_data = {'date': ['2014-06-01T01:21:38.004053', '2014-06-02T01:21:38.004053', '2014-06-03T01:21:38.004053'],\n",
41+
" 'score': [25, 94, 57]}\n",
42+
"df = pd.DataFrame(raw_data, columns = ['date', 'score'])\n",
43+
"df"
44+
],
45+
"language": "python",
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"html": [
50+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
51+
"<table border=\"1\" class=\"dataframe\">\n",
52+
" <thead>\n",
53+
" <tr style=\"text-align: right;\">\n",
54+
" <th></th>\n",
55+
" <th>date</th>\n",
56+
" <th>score</th>\n",
57+
" </tr>\n",
58+
" </thead>\n",
59+
" <tbody>\n",
60+
" <tr>\n",
61+
" <th>0</th>\n",
62+
" <td> 2014-06-01T01:21:38.004053</td>\n",
63+
" <td> 25</td>\n",
64+
" </tr>\n",
65+
" <tr>\n",
66+
" <th>1</th>\n",
67+
" <td> 2014-06-02T01:21:38.004053</td>\n",
68+
" <td> 94</td>\n",
69+
" </tr>\n",
70+
" <tr>\n",
71+
" <th>2</th>\n",
72+
" <td> 2014-06-03T01:21:38.004053</td>\n",
73+
" <td> 57</td>\n",
74+
" </tr>\n",
75+
" </tbody>\n",
76+
"</table>\n",
77+
"</div>"
78+
],
79+
"metadata": {},
80+
"output_type": "pyout",
81+
"prompt_number": 12,
82+
"text": [
83+
" date score\n",
84+
"0 2014-06-01T01:21:38.004053 25\n",
85+
"1 2014-06-02T01:21:38.004053 94\n",
86+
"2 2014-06-03T01:21:38.004053 57"
87+
]
88+
}
89+
],
90+
"prompt_number": 12
91+
},
92+
{
93+
"cell_type": "code",
94+
"collapsed": false,
95+
"input": [
96+
"# Transpose the dataset, so that the index (in this case the names) are columns\n",
97+
"df[\"date\"] = pd.to_datetime(df[\"date\"])"
98+
],
99+
"language": "python",
100+
"metadata": {},
101+
"outputs": [],
102+
"prompt_number": 13
103+
},
104+
{
105+
"cell_type": "code",
106+
"collapsed": false,
107+
"input": [
108+
"df = df.set_index(df[\"date\"])"
109+
],
110+
"language": "python",
111+
"metadata": {},
112+
"outputs": [],
113+
"prompt_number": 14
114+
},
115+
{
116+
"cell_type": "code",
117+
"collapsed": false,
118+
"input": [
119+
"df"
120+
],
121+
"language": "python",
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"html": [
126+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
127+
"<table border=\"1\" class=\"dataframe\">\n",
128+
" <thead>\n",
129+
" <tr style=\"text-align: right;\">\n",
130+
" <th></th>\n",
131+
" <th>date</th>\n",
132+
" <th>score</th>\n",
133+
" </tr>\n",
134+
" <tr>\n",
135+
" <th>date</th>\n",
136+
" <th></th>\n",
137+
" <th></th>\n",
138+
" </tr>\n",
139+
" </thead>\n",
140+
" <tbody>\n",
141+
" <tr>\n",
142+
" <th>2014-06-01 01:21:38.004053</th>\n",
143+
" <td>2014-06-01 01:21:38.004053</td>\n",
144+
" <td> 25</td>\n",
145+
" </tr>\n",
146+
" <tr>\n",
147+
" <th>2014-06-02 01:21:38.004053</th>\n",
148+
" <td>2014-06-02 01:21:38.004053</td>\n",
149+
" <td> 94</td>\n",
150+
" </tr>\n",
151+
" <tr>\n",
152+
" <th>2014-06-03 01:21:38.004053</th>\n",
153+
" <td>2014-06-03 01:21:38.004053</td>\n",
154+
" <td> 57</td>\n",
155+
" </tr>\n",
156+
" </tbody>\n",
157+
"</table>\n",
158+
"</div>"
159+
],
160+
"metadata": {},
161+
"output_type": "pyout",
162+
"prompt_number": 15,
163+
"text": [
164+
" date score\n",
165+
"date \n",
166+
"2014-06-01 01:21:38.004053 2014-06-01 01:21:38.004053 25\n",
167+
"2014-06-02 01:21:38.004053 2014-06-02 01:21:38.004053 94\n",
168+
"2014-06-03 01:21:38.004053 2014-06-03 01:21:38.004053 57"
169+
]
170+
}
171+
],
172+
"prompt_number": 15
173+
},
174+
{
175+
"cell_type": "code",
176+
"collapsed": false,
177+
"input": [],
178+
"language": "python",
179+
"metadata": {},
180+
"outputs": [],
181+
"prompt_number": 15
182+
}
183+
],
184+
"metadata": {}
185+
}
186+
]
187+
}

0 commit comments

Comments
 (0)