时间段与瞬时之间的时差

时间:2018-09-25 13:33:19

标签: pandas apply

我有一些时间段(df_A)和一些时间瞬间(df_B):

import pandas   as pd
import numpy    as np
import datetime as dt
from   datetime import timedelta

# Data
df_A = pd.DataFrame({'A1': [dt.datetime(2017,1,5,9,8),   dt.datetime(2017,1,5,9,9),  dt.datetime(2017,1,7,9,19), dt.datetime(2017,1,7,9,19),  dt.datetime(2017,1,7,9,19), dt.datetime(2017,2,7,9,19), dt.datetime(2017,2,7,9,19)],
                     'A2': [dt.datetime(2017,1,5,9,9),   dt.datetime(2017,1,5,9,12), dt.datetime(2017,1,7,9,26), dt.datetime(2017,1,7,9,20),  dt.datetime(2017,1,7,9,21), dt.datetime(2017,2,7,9,23), dt.datetime(2017,2,7,9,25)]})

df_B = pd.DataFrame({ 'B': [dt.datetime(2017,1,6,14,45), dt.datetime(2017,1,4,3,31), dt.datetime(2017,1,7,3,31), dt.datetime(2017,1,7,14,57), dt.datetime(2017,1,9,14,57)]})

我可以将它们搭配在一起

# Define an Extra Margin
M = dt.timedelta(days = 10)

df_A["A1X"] = df_A["A1"] + M
df_A["A2X"] = df_A["A2"] - M

# Match
Bv = df_B .B  .values
A1 = df_A .A1X.values
A2 = df_A .A2X.values

i, j = np.where((Bv[:, None] >= A1) & (Bv[:, None] <= A2))

df_C = pd.DataFrame(np.column_stack([df_B .values[i], df_A .values[j]]),
                    columns = df_B .columns .append (df_A.columns))

我想找到每个时间段和与其匹配的时刻之间的时差。我的意思是

如果B在A1和A2之间

则dT = 0

我试图这样做:

# Calculate dT
def time(A1,A2,B):
    if   df_C["B"] < df_C["A1"]:
        return df_C["A1"].subtract(df_C["B"])
    elif df_C["B"] > df_C["A2"]:
        return df_C["B"].subtract(df_C["A2"])
    else:
        return 0

df_C['dT'] = df_C.apply(time)

我收到“ ValueError:无法设置没有定义索引和无法转换为Series的值的框架”

1 个答案:

答案 0 :(得分:1)

因此,我发现了两个修复程序:

  1. 您正在将M添加到较低的值,然后从较高的值中减去。更改为:

    df_A['A1X'] = df_A['A1'] - M
    df_A['A2X'] = df_A['A2'] + M
    
  2. 您一次只将数据帧的一行传递给time函数,因此应该是这样的:

    def time(row):
        if row['B'] < row['A1']:
            return row['A1'] - row['B']
        elif row['B'] > row['A2']:
            return row['B'] - row['A2']
        else:
            return 0
    

    然后您可以这样称呼它:

    df_C['dT'] = df_C.apply(time, axis=1)
    
相关问题