Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract hour, minute and second from Series filled with datetime.time values

Data:

0    09:30:38
1    13:40:27
2    18:05:24
3    04:58:08
4    09:00:09

Essentially what I'd like to do is split this into three columns [hour, minute, second]

I've tried the following code but none seem to be working:

train_sample.time.hour
AttributeError: 'Series' object has no attribute 'hour'

train_sample.time.dt.hour
AttributeError: Can only use .dt accessor with datetimelike values 

pd.DatetimeIndex(train_sample.time).hour
TypeError: <class 'datetime.time'> is not convertible to datetime

This seems so simple but I can't figure it out. Any help would be much appreciated.

like image 620
madsthaks Avatar asked Mar 15 '18 11:03

madsthaks


3 Answers

Use list comprehension with extract attributes of times:

import datetime as datetime

df = pd.DataFrame({'time': [datetime.time(9, 30, 38), 
                            datetime.time(13, 40, 27), 
                            datetime.time(18, 5, 24),
                            datetime.time(4, 58, 8), 
                            datetime.time(9, 0, 9)]})

print (df)
       time
0  09:30:38
1  13:40:27
2  18:05:24
3  04:58:08
4  09:00:09

df[['h','m','s']] = pd.DataFrame([(x.hour, x.minute, x.second) for x in df['time']])

Or convert to strings, split and convert to int:

df[['h','m','s']] = df['time'].astype(str).str.split(':', expand=True).astype(int)

print (df)
       time   h   m   s
0  09:30:38   9  30  38
1  13:40:27  13  40  27
2  18:05:24  18   5  24
3  04:58:08   4  58   8
4  09:00:09   9   0   9
like image 71
jezrael Avatar answered Oct 17 '22 02:10

jezrael


Splitting using : and creating a dataframe with each of the split as separate column values.

import pandas as pd

d = {0: '09:30:38', 
     1: '13:40:27', 
     2: '18:05:24',
     3: '04:58:08',
     4: '09:00:09'}

df = pd.DataFrame([v.split(':') for v in d.values()], columns=['hour', 'minute', 'second'])
print(df)

Result:

  hour minute second
0   09     30     38                                        
1   13     40     27                                        
2   18     05     24                                        
3   04     58     08                                        
4   09     00     09      
like image 2
Austin Avatar answered Oct 17 '22 00:10

Austin


Looks like your problem is really just missing the datetime accessor Use dt at the end of your Series then you can extract with the .hour method

train_sample['hour'] = train_sample.dt.hour
train_sample['minute'] = train_sample.dt.minute
train_sample['second'] = train_sample.dt.second
like image 2
gojandrooo Avatar answered Oct 17 '22 00:10

gojandrooo