pandas 如何从填充了 datetime.time 值的系列中提取小时、分钟和秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49298488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract hour, minute and second from Series filled with datetime.time values
提问by madsthaks
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.
这看起来很简单,但我无法弄清楚。任何帮助将非常感激。
回答by jezrael
Use list comprehension with extract attributes of time
s:
对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
:
或转换为string
s,拆分并转换为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
回答by Austin
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
回答by gojandrooo
Looks like your problem is really just missing the datetime accessorUse dt
at the end of your Series then you can extract with the .hour method
看起来您的问题实际上只是缺少日期时间访问器dt
在系列末尾使用,然后您可以使用 .hour 方法提取
train_sample['hour'] = train_sample.dt.hour
train_sample['minute'] = train_sample.dt.minute
train_sample['second'] = train_sample.dt.second
回答by jpp
One way is to convert to timedelta
and extract via pd.Series.dt.components
:
一种方法是timedelta
通过以下方式转换和提取pd.Series.dt.components
:
df[['hour','minute','second']] = pd.to_timedelta(df['time']).dt.components.iloc[:, 1:4]
Result
结果
time hour minute second
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