计算同一熊猫列中两个不同值之间的时间

时间:2020-08-16 08:29:46

标签: python pandas datetime

我有如下数据

Device     Time        Condition
D1  01/11/2019 00:00    issue
D1  01/11/2019 00:15    issue
D1  01/11/2019 00:30    issue
D1  01/11/2019 00:45    issue
D1  01/11/2019 01:00    issue
D1  01/11/2019 01:15    Resolved
D1  01/11/2019 01:30    Resolved
D2  01/11/2019 01:45    issue
D2  01/11/2019 02:00    Resolved
D1  01/11/2019 01:45    issue
D1  01/11/2019 02:00    Resolved

我需要创建一个新列,以查找第一个问题和第一个解决问题之间的时间。 我需要一个groupby声明,该声明将保留所有问题的第一个问题,并保留所有问题的第一个问题。然后找到时间-当我按设备分组并设置条件时,每个设备只保留了一个问题。

所需的输出如下所示

Device  Time    Condition   durationTofix
D1  01/11/2019 00:00    issue   
D1  01/11/2019 00:15    issue   
D1  01/11/2019 00:30    issue   
D1  01/11/2019 00:45    issue   
D1  01/11/2019 01:00    issue   
D1  01/11/2019 01:15    Resolved    01:15
D1  01/11/2019 01:30    Resolved    
D2  01/11/2019 01:45    issue   
D2  01/11/2019 02:00    Resolved    00:15
D1  01/11/2019 01:45    issue   
D1  01/11/2019 02:00    Resolved    00:15

由于groupby Device and Condition不够,我认为无法创建索引列

data["index"] = data.groupby(['Device','condition']).??? #Something like cumcount() but it is not cumcount in this case

然后使用数据透视表进行时间计算

H = data.pivot_table(index=['index','Device'], columns=['condition'], values='Timestamp',aggfunc=lambda x: x)
H['durationTofix'] = H['Resolved']- H['issue']

2 个答案:

答案 0 :(得分:1)

在每个Resolved之前的每个连续组中,Device之前始终至少存在一个问题:

#converting to datetimes
df['Time'] = pd.to_datetime(df['Time'])

#consetutive groups
g = df['Device'].ne(df['Device'].shift()).cumsum()
#test issue values
m = df['Condition'].eq('issue')
#replace not issue to missing values
i = df['Time'].where(m)

#get first duplicated rows by consecutive groups and condition column
mask = ~df.assign(g=g,i=i).duplicated(['g','Condition']) 
#forward filling Time by first issue per groups
s = df['Time'].where(mask & m).groupby(g).ffill()

#subtract and filter only first Resolved per groups
df['durationTofix'] = df['Time'].sub(s).where(mask & df['Condition'].eq('Resolved'))
print (df)
   Device                Time Condition durationTofix
0      D1 2019-01-11 00:00:00     issue           NaT
1      D1 2019-01-11 00:15:00     issue           NaT
2      D1 2019-01-11 00:30:00     issue           NaT
3      D1 2019-01-11 00:45:00     issue           NaT
4      D1 2019-01-11 01:00:00     issue           NaT
5      D1 2019-01-11 01:15:00  Resolved      01:15:00
6      D1 2019-01-11 01:30:00  Resolved           NaT
7      D2 2019-01-11 01:45:00     issue           NaT
8      D2 2019-01-11 02:00:00  Resolved      00:15:00
9      D1 2019-01-11 01:45:00     issue           NaT
10     D1 2019-01-11 02:00:00  Resolved      00:15:00

答案 1 :(得分:1)

最大的问题是如何正确分组/解决问题,这可以通过颠倒的cumsum完成:

df["Time"] = pd.to_datetime(df["Time"])

df["group"] = (df["Condition"].eq("Resolved")&df["Condition"].shift(-1).eq("issue"))[::-1].cumsum()[::-1]

df["diff"] = (df[~df.duplicated(["Condition","group"])].groupby("group")["Time"].transform(lambda d: d.diff()))

print (df)

   Device                Time Condition  group     diff
0      D1 2019-01-11 00:00:00     issue      2      NaT
1      D1 2019-01-11 00:15:00     issue      2      NaT
2      D1 2019-01-11 00:30:00     issue      2      NaT
3      D1 2019-01-11 00:45:00     issue      2      NaT
4      D1 2019-01-11 01:00:00     issue      2      NaT
5      D1 2019-01-11 01:15:00  Resolved      2 01:15:00
6      D1 2019-01-11 01:30:00  Resolved      2      NaT
7      D2 2019-01-11 01:45:00     issue      1      NaT
8      D2 2019-01-11 02:00:00  Resolved      1 00:15:00
9      D1 2019-01-11 01:45:00     issue      0      NaT
10     D1 2019-01-11 02:00:00  Resolved      0 00:15:00