如何从填充有 datetime.time 值的系列中提取小时、分钟和秒

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

数据:

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

基本上我想做的是将其分成三列[小时、分钟、秒]

我尝试了以下代码,但 none 似乎有效:

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

这看起来很简单,但我想不通。任何帮助将非常感激。

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']])

或转换为strings,拆分并转换为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

一种方法是转换为 timedelta 并通过 pd.Series.dt.components:

提取
df[['hour','minute','second']] = pd.to_timedelta(df['time']).dt.components.iloc[:, 1:4]

结果

       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

使用 : 拆分并创建数据框,每个拆分作为单独的列值。

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)

结果:

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

看起来你的问题真的只是缺少 datetime accessor 在系列的末尾使用 dt 然后你可以使用 .hour 方法提取

train_sample['hour'] = train_sample.dt.hour
train_sample['minute'] = train_sample.dt.minute
train_sample['second'] = train_sample.dt.second