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.
Use list comprehension with extract attributes of time
s:
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 string
s, 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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With