以下代码生成df:
import pandas as pd
import random
from datetime import timedelta
def randomTime():
rtime = int(random.random()*86400)
hours = int(rtime/3600)
minutes = int((rtime - hours*3600)/60)
seconds = rtime - hours*3600 - minutes*60
time_string = '%02d:%02d:%02d' % (hours, minutes, seconds)
return time_string
time = [randomTime() for _ in range(8)]
k = 5
N = 8
d = ({'Time' : (time),
'Events' : ['ABC','DEF','GHI','JKL','ABC','DEF','GHI','JKL'],
'Number1' : ['xx','xx',1,'xx','xx','xx',2,'xx'],
'Number2' : ['xx',1,'xx',1,'xx',2,'xx',2]})
df = pd.DataFrame(data=d)
我正在尝试为df中选定的时间戳行添加时间。我试图改变最后4个时间戳来增加3个小时。目前,它正在制作字符串。
df.iloc[4:8,3] = pd.TimedeltaIndex(df.iloc[4:8,3]) + timedelta(hours=3)
print(df)
Output:
Events Number1 Number2 Time
0 ABC xx xx 14:25:51
1 DEF xx 1 10:02:32
2 GHI 1 xx 01:23:32
3 JKL xx 1 07:27:42
4 ABC xx xx 74325000000000
5 DEF xx 2 38992000000000
6 GHI 2 xx 19158000000000
7 JKL xx 2 26746000000000
我不确定这是否因为此处的计算中添加了日期?我不确定我做错了什么。
答案 0 :(得分:0)
您的Time
列是一列字符串。我建议将整个列转换为timedelta
首先,然后再添加3小时。否则,你最终会混合dtypes(字符串和timedelta),并且会发生奇怪的事情。
df['Time'] = pd.to_timedelta(df.Time, errors='coerce')
df.iloc[4:8, 3] += pd.Timedelta(hours=3)
df
Events Number1 Number2 Time
0 ABC xx xx 0 days 13:51:35
1 DEF xx 1 0 days 23:14:11
2 GHI 1 xx 0 days 02:16:28
3 JKL xx 1 0 days 05:25:40
4 ABC xx xx 0 days 15:40:15
5 DEF xx 2 0 days 04:05:26
6 GHI 2 xx 0 days 15:48:06
7 JKL xx 2 1 days 00:12:30