将以秒为单位的负持续时间转换为负%H:%M:%S.%f

时间:2019-10-17 13:46:05

标签: python pandas timestamp timedelta

我正在使用Pandas计算两个持续时间之间的时间差。

函数是:

def time_calc(dur1, dur2):
    date1 = pd.to_datetime(pd.Series(dur2))
    date2 = pd.to_datetime(pd.Series(dur1))

    df = pd.DataFrame(dict(ID = ids, DUR1 = date2, DUR2 = date1))
    df1 = pd.DataFrame(dict(ID = ids, Duration1 = date2, Duration2 = date1))
    df1['Duration1'] = df['DUR1'].dt.strftime('%H:%M:%S.%f')
    df1['Duration2'] = df['DUR2'].dt.strftime('%H:%M:%S.%f')
    cols = df.columns.tolist()
    cols = ['ID', 'DUR1', 'DUR2']
    df = df[cols]
    df['diff_seconds'] = df['DUR2'] - df['DUR1']
    df['diff_seconds'] = df['diff_seconds']/np.timedelta64(1,'s')
    df['TimeDelta'] = df['diff_seconds'].apply(lambda d: str(datetime.timedelta(seconds=abs(d))))
    df3 = df1.merge(df, on='ID')
    cols = df3.columns.tolist()
    cols = ['ID', 'Duration1', 'Duration2', 'TimeDelta', 'diff_seconds']
    df3 = df3[cols]
    print(df3)

数学为:Duration2-Duration1=TimeDelta

该函数很好地完成了

Duration1        Duration2         TimeDelta           diff_seconds
00:00:23.999891  00:00:25.102076   0:00:01.102185      1.102185
00:00:43.079173  00:00:44.621481   0:00:01.542308      1.542308

但是,当Duration2

Duration1        Duration2         TimeDelta           diff_seconds
00:05:03.744332  00:04:58.008081   0:00:05.736251     -5.736251

所以我需要执行的功能是将TimeDelta转换为负值,如下所示:

Duration1        Duration2         TimeDelta           diff_seconds
00:05:03.744332  00:04:58.008081   -0:00:05.736251     -5.736251

我想我需要以另一种方式转换“ TimeDelta”,但我的所有尝试都没有用。

如果有人可以帮助我,我将非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

制作一对一的时间戳选择逻辑,并将时间戳传递给“ time_convert”功能

df['diff_seconds'] = df['DUR2'] - df['DUR1']
df['diff_seconds'] = df['diff_seconds']/np.timedelta64(1,'s')

for i in df['diff_seconds']:
    df['TimeDelta'] = time_convert(i)

如果秒为负数,time_convert函数仅将“-”附加到格式化的时间戳记上。

def time_convert(d):
    if d > 0:
        lst.append(str(datetime.timedelta(seconds=d)))
    else:
        lst.append('-' + str(datetime.timedelta(seconds=abs(d))))

然后,我刚刚使用lst创建了新的数据框,并将其合并在一起

df_t = pd.DataFrame(dict(ALERTS = alerts, TimeDelta = lst))
df_f = df_t.merge(df3, on='ID')

希望这对某人有帮助。