tomzko
tomzko

Reputation: 77

Assign time element of DateTimeIndex to new column

im working with the below DataFrame and want to access only the time (not date) of my DateTimeIndex:

                    idle wheel  Induce wheel axial  radial
tiempo          
5/30/2016 19:37:46  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:47  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:48  -1,099.12   -1,048.78   -477.21
5/30/2016 19:37:49  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:50  -1,099.12   -1,048.78   -477.21
5/30/2016 19:37:51  -1,099.12   -1,048.78   -477.35
5/30/2016 19:37:52  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:53  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:54  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:55  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:56  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:57  -1,099.12   -1,048.78   -476.98

I want to keep only the 19:..:.., not the date part. I've been searching but couldn't find any solution.

Upvotes: 2

Views: 363

Answers (1)

Stefan
Stefan

Reputation: 42885

Use .time on your index 'tiempo' :

df['time'] = df.index.time

or, if 'tiempo' is a column use

df['time'] = df.tiempo.dt.time

to get:

                tiempo  idle wheel  Induce wheel axial    radial      time
0  2016-05-30 19:37:46    -1099.12    -1048.78           -477.13  19:37:46
1  2016-05-30 19:37:47    -1099.12    -1048.78           -476.98  19:37:47
2  2016-05-30 19:37:48    -1099.12    -1048.78           -477.21  19:37:48
3  2016-05-30 19:37:49    -1099.12    -1048.78           -477.13  19:37:49
4  2016-05-30 19:37:50    -1099.12    -1048.78           -477.21  19:37:50
5  2016-05-30 19:37:51    -1099.12    -1048.78           -477.35  19:37:51
6  2016-05-30 19:37:52    -1099.12    -1048.78           -477.13  19:37:52
7  2016-05-30 19:37:53    -1099.12    -1048.78           -476.98  19:37:53
8  2016-05-30 19:37:54    -1099.12    -1048.78           -476.98  19:37:54
9  2016-05-30 19:37:55    -1099.12    -1048.78           -476.98  19:37:55
10 2016-05-30 19:37:56    -1099.12    -1048.78           -476.98  19:37:56
11 2016-05-30 19:37:57    -1099.12    -1048.78           -476.98  19:37:57

Upvotes: 2

Related Questions