我有一些时刻(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/