起初我還以爲那裏是由於沒有綠色蜱在這裏沒有正確答案。但正如傑夫在評論中指出的那樣,我錯了。
無論哪種方式是我的貢獻。
首先,很明顯,使得datetime.time
成timedelta
df['delta'] = (pd.to_timedelta(df.time2.astype(str)) - pd.to_timedelta(df.time1.astype(str)))
time1 time2 delta
0 13:00:07.294234 13:00:07.294234 00:00:00
1 14:00:07.294234 14:00:07.394234 00:00:00.100000
2 15:00:07.294234 15:00:07.494234 00:00:00.200000
3 16:00:07.294234 16:00:07.694234 00:00:00.400000
現在我們有timedelta
我們可以簡單地通過一微秒除以它獲得的微秒數。
df['microsecond_delta'] = df.delta/pd.np.timedelta64(1, 'us')
time1 time2 delta microsecond_delta
0 13:00:07.294234 13:00:07.294234 00:00:00 0
1 14:00:07.294234 14:00:07.394234 00:00:00.100000 100000
2 15:00:07.294234 15:00:07.494234 00:00:00.200000 200000
3 16:00:07.294234 16:00:07.694234 00:00:00.400000 400000
我必須補充一點,這是非常直觀的,但它似乎是唯一的方法。似乎沒有辦法直接訪問毫秒。我通過應用lambda函數類似的嘗試:
df.delta.apply(lambda x: x.microseconds)
AttributeError: 'numpy.timedelta64' object has no attribute 'microseconds'
同樣是seconds
,nanoseconds
,milliseconds
等真的...
還可以''np.timedelta64分(1, '我們')' ' – Jeff