python - 时间段和瞬时 1 之间的时间差

标签 python pandas apply

我有一些时刻(A、B)和一些时间段(C-D)。我想找到每个时间段和与其匹配的时刻之间的时差。我的意思是:

如果时间(A 或 B)在 C 和 D 之间

然后 dT = 0

我尝试过这样做:

df = pd.DataFrame({'A': [dt.datetime(2017,1,6),      dt.datetime(2017,1,4)],
                   'B': [dt.datetime(2017,1,7),      dt.datetime(2017,1,5)],
                   'C': [dt.datetime(2017,1,6,12,3), dt.datetime(2017,1,6,13,3)],
                   'D': [dt.datetime(2017,1,8,12,3), dt.datetime(2017,1,8,14,3)]})

# Calculate the time difference
def dT(Time, on, off):
    if Time < on:
        return on - Time
    elif Time > off:
        return Time - off
    else:
        return 0
dT = np.vectorize(dT)

df['dT_A'] = dT(df['A'], df['C'], df['D'])
df['dT_B'] = dT(df['B'], df['C'], df['D'])

# Change the time difference to a float
def floa(dT):
    if dT == 0:
        return 0
    else:
        return dT / timedelta (days=1)
floa = np.vectorize(floa)

df['dT_A'] = floa(df['dT_A'])
df['dT_B'] = floa(df['dT_B'])

它计算了dt_A,但随后它给了我这个错误:

OverflowError: Python int too large to convert to C long

最佳答案

尽管有它的名字,np.vectorize 并不是矢量化的 - 它在循环中工作。因此,如果可以的话,最好在向量上工作,幸运的是,你想要的东西在“ Vanilla ”Pandas 中很容易做到:

import datetime as dt

df = pd.DataFrame({'A': [dt.datetime(2017,1,6),      dt.datetime(2017,1,4)],
                   'B': [dt.datetime(2017,1,7),      dt.datetime(2017,1,5)],
                   'C': [dt.datetime(2017,1,6,12,3), dt.datetime(2017,1,6,13,3)],
                   'D': [dt.datetime(2017,1,8,12,3), dt.datetime(2017,1,8,14,3)]})
# default is 0
df['dT_A'] = 0
df['dT_B'] = 0

df.loc[df.A < df.C, 'dT_A'] = (df.C - df.A) .loc[df.A < df.C]
df.loc[df.A > df.D, 'dT_A'] = (df.A - df.D) .loc[df.A > df.D]

df.loc[df.B < df.C, 'dT_B'] = (df.C - df.B) .loc[df.B < df.C]
df.loc[df.B > df.D, 'dT_B'] = (df.B - df.D) .loc[df.B > df.D]

# convert timedelta to number of days, to float
df['dT_A'] = df.dT_A / dt.timedelta(days=1)
df['dT_B'] = df.dT_B / dt.timedelta(days=1)

关于python - 时间段和瞬时 1 之间的时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55006135/

相关文章:

python - 重定向到 webapp2 中的上一个 url

python - python 中的嵌套列表和 for 循环程序给了我一个疯狂的结果

python - 使用 date_range 时如何使 x 轴更详细

python - 获取 pandas 最后一分钟的所有行

python - Pandas 将行与条件进行比较

python - 应用操作连接数据帧中的某些行重新调整无

python - 计算 DataFrame 每一行中 Series 中项目的出现次数

python - 在 ubuntu 上为 arm board(beaglebone black)交叉编译 opencv 2.4.5-链接 CXX 共享库错误为 92%

r - 使用 R 中的一个数据评估多个函数

python - 过滤具有 0 和 1 序列的列中的行模式