1

我有两个熊猫数据框,每个数据框有两列:测量值和时间戳。我需要将测量值的一阶差相乘,但前提是两个测量间隔之间存在时间重叠。随着数据框的大小变大,我怎样才能有效地做到这一点?例子:

dfA
      mesA   timeA
0     125    2015-01-14 04:44:49     
1     100    2015-01-14 05:16:23
2     115    2015-01-14 08:57:10     

dfB
      mesB    timeB
0     140     2015-01-14 00:13:17
1     145     2015-01-14 08:52:01
2     120     2015-01-14 11:31:44

在这里,我会相乘,(100-125)*(145-140)因为间隔[04:44:49, 05:16:23]和之间存在时间重叠[00:13:17, 08:52:01],但没有(100-125)(120-145),因为没有时间重叠。同样,我也会有,(115-100)*(145-140)但也有(115-100)*(120-145),因为两者都有时间重叠。

最后,我必须将所有相关产品汇总为一个值,因此结果不必是数据框。在这种情况下:

s = (100-125)*(145-140)+(115-100)*(145-140)+(115-100)*(120-145) = -425

我目前的解决方案:

s = 0
for i in range(1, len(dfA)):
    startA = dfA['timeA'][i-1]
    endA = dfA['timeA'][i]
    for j in range(1, len(dfB)):
        startB = dfB['timeB'][j-1]
        endB = dfB['timeB'][j]
        if (endB>startA) & (startB<endA):
            s+=(dfA['mesA'][i]-dfA['mesA'][i-1])*(dfB['mesB'][j]-dfB['mesB'][j-1])

尽管它似乎有效,但效率非常低,并且对于非常大的数据集变得不切实际。我相信它可以更有效地矢量化,也许使用numexpr,但我仍然没有找到方法。

编辑:其他数据

    mesA  timeA
0   125   2015-01-14 05:54:03
1   100   2015-01-14 11:39:53
2   115   2015-01-14 23:58:13
    mesB  timeB
0   110   2015-01-14 10:58:32
1   120   2015-01-14 13:30:00
2   135   2015-01-14 22:29:26

s = 125
4

1 回答 1

1

编辑:原来的答案不起作用,所以我想出了另一个不是矢量化但需要按日期排序的版本。

arrA = dfA.timeA.to_numpy()
startA, endA = arrA[0], arrA[1]
arr_mesA = dfA.mesA.diff().to_numpy()
mesA = arr_mesA[1]

arrB = dfB.timeB.to_numpy()
startB, endB = arrB[0], arrB[1]
arr_mesB = dfB.mesB.diff().to_numpy()
mesB = arr_mesB[1]

s = 0
i, j = 1, 1
imax = len(dfA)-1
jmax = len(dfB)-1
while True:
    if (endB>startA) & (startB<endA):
        s+=mesA*mesB
    if (endB>endA) and (i<imax):
        i+=1
        startA, endA, mesA= endA, arrA[i], arr_mesA[i]
    elif j<jmax:
        j+=1
        startB, endB, mesB = endB, arrB[j], arr_mesB[j]
    else:
        break

原来不工作的答案

这个想法是根据两个数据框中pd.cut的值来分类,dfB['timeB']以查看它们可以重叠的位置。然后计算diffin 测量值。merge类别上的两个数据框,最后相乘和sum整个事情

# create bins
bins_dates = [min(dfB['timeB'].min(), dfA['timeA'].min())-pd.DateOffset(hours=1)]\
             + dfB['timeB'].tolist()\
             + [max(dfB['timeB'].max(), dfA['timeA'].max())+pd.DateOffset(hours=1)]

# work on dfB
dfB['cat'] = pd.cut(dfB['timeB'], bins=bins_dates,
                    labels=range(len(bins_dates)-1), right=False)
dfB['deltaB'] = -dfB['mesB'].diff(-1).ffill()

# work on dfA
dfA['cat'] = pd.cut(dfA['timeA'], bins=bins_dates,
                    labels=range(len(bins_dates)-1), right=False)
# need to calcualte delta for both start and end of intervals
dfA['deltaAStart'] = -dfA['mesA'].diff(-1)
dfA['deltaAEnd'] = dfA['mesA'].diff().mask(dfA['cat'].astype(float).diff().eq(0))
# in the above method, for the end of interval, use a mask to not count twice 
# intervals that are fully included in one interval of B

# then merge and calcualte the multiplication you are after
df_ = dfB[['cat', 'deltaB']].merge(dfA[['cat','deltaAStart', 'deltaAEnd']])
s = (df_['deltaB'].to_numpy()[:,None]*df_[['deltaAStart', 'deltaAEnd']]).sum().sum()
print (s)
#-425.0
于 2020-05-14T01:13:12.603 回答