使用Lambda进行熊猫时间转换

时间:2016-04-04 10:44:55

标签: python pandas lambda

我有以下数据框df

no.        site_number        date              time  Class  Speed_KPH
0                  11  2016-02-27  00:00:13.0000000  Short         43
1                  11  2016-02-27  00:02:15.0000000  Short         45
2                  11  2016-02-27  00:00:28.0000000  Short         31
3                  11  2016-02-27  00:03:28.0000000  Short         31

我希望从time_slot列创建一个新列time,以便它给出给定小时的结束时间。

no.        site_number        date              time  Class  Speed_KPH      Hour_slot
    0                  11  2016-02-27  00:00:13.0000000  Short         43    1
    1                  11  2016-02-27  00:02:15.0000000  Short         45    3
    2                  11  2016-02-27  00:00:28.0000000  Short         31    1
    3                  11  2016-02-27  00:03:28.0000000  Short         31    4

我定义了一个函数将时间转换为一小时,然后使用lambda演算来定义一个新列time_slot

def time_slot_convert(time):
    return (time.hour()) + 1

df['time_slot'] = df.apply(lambda row: time_slot_convert(row['time']), axis =1)

但是,我收到此错误:

AttributeError: ("'str' object has no attribute 'hour'", u'occurred at index 0')

2 个答案:

答案 0 :(得分:1)

您需要先转换列to_datetime

如果00:00:13.0000000Hours:Minutes:Seconds,那么您可以使用dt.minute,而不是dt.hour

df['time_slot'] = pd.to_datetime(df['time']).dt.minute + 1
print df
     site_number       date              time  Class  Speed_KPH  time_slot
no.                                                                       
0             11 2016-02-27  00:00:13.0000000  Short         43          1
1             11 2016-02-27  00:02:15.0000000  Short         45          3
2             11 2016-02-27  00:00:28.0000000  Short         31          1
3             11 2016-02-27  00:03:28.0000000  Short         31          4


df['time_slot'] = pd.to_datetime(df['time']).dt.hour + 1
print df
     site_number       date              time  Class  Speed_KPH  time_slot
no.                                                                       
0             11 2016-02-27  00:00:13.0000000  Short         43          1
1             11 2016-02-27  00:02:15.0000000  Short         45          1
2             11 2016-02-27  00:00:28.0000000  Short         31          1
3             11 2016-02-27  00:03:28.0000000  Short         31          1

如果您需要apply功能:

def time_slot_convert(time):
    return (time.minute + 1)

df['time_slot'] = pd.to_datetime(df['time']).apply(time_slot_convert)
print df
     site_number       date              time  Class  Speed_KPH  time_slot
no.                                                                       
0             11 2016-02-27  00:00:13.0000000  Short         43          1
1             11 2016-02-27  00:02:15.0000000  Short         45          3
2             11 2016-02-27  00:00:28.0000000  Short         31          1
3             11 2016-02-27  00:03:28.0000000  Short         31          4

或者应用lambda

def time_slot_convert(time):
    return (time.minute + 1)

df['time_slot']=df.apply(lambda row: time_slot_convert(pd.to_datetime(row['time'])),axis=1)
print df
     site_number       date              time  Class  Speed_KPH  time_slot
no.                                                                       
0             11 2016-02-27  00:00:13.0000000  Short         43          1
1             11 2016-02-27  00:02:15.0000000  Short         45          3
2             11 2016-02-27  00:00:28.0000000  Short         31          1
3             11 2016-02-27  00:03:28.0000000  Short         31          4

答案 1 :(得分:0)

你可以尝试:

import time
def time_slot_convert(time):
    time = time.strptime(x, "%H:%M:%S.0000000")
    return time.tm_hour + 1