在数据框的一栏中进行日期时间的计算

时间:2018-11-08 06:24:18

标签: python pandas datetime

假设我在数据框中有两列,包括事件的概率和剩余时间。

      prob          time         
0   0.975909   0 days 00:00:00   
1   0.957819   0 days 01:00:00   
2   0.937498   0 days 02:00:00   
3   0.912779   0 days 03:00:00   
4   0.894139   0 days 04:00:00   
5   0.873184   0 days 05:00:00   
6   0.847748   0 days 06:00:00   
7   0.828572   0 days 07:00:00   
8   0.807029   0 days 08:00:00   
9   0.780847   0 days 09:00:00   
10  0.761082   0 days 10:00:00   
11  0.738855   0 days 11:00:00   
12  0.711733   0 days 12:00:00   

我想计算确切的时间和日期,例如,输入日期和时间以及概率,例如:

# Type the date of input data 
i = datetime.datetime.now() #e.g. 2018-01-01 00:00:00

# Type the expected probability 
exprob = 0.80

我需要的输出是以下结果: 用'exprob'(0.80)-> 0.80709找到最接近的概率,然后计算'i'+与0.80709相关的时间= 2018-01-01 08:00:00

2 个答案:

答案 0 :(得分:0)

您可以使用idxmin查找df['prob']exprob之间的最小差的索引,然后找到Timedelta并将其添加到日期{{1} }:

i

答案 1 :(得分:0)

使用argsort(),我们可以像下面这样得到它。

input = 0.80
i = datetime.now()
next_time = i + df.ix[(df['prob']-input).abs().argsort()[:1]]['time']

完整的示例是

import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame(columns = ['prob', 'time'])
df.loc[len(df)] = [0.975909, timedelta(hours=0, minutes=0, seconds=0)]
df.loc[len(df)] = [0.957819, timedelta(hours=1, minutes=0, seconds=0)]
df.loc[len(df)] = [0.937498, timedelta(hours=2, minutes=0, seconds=0)]
df.loc[len(df)] = [0.912779, timedelta(hours=3, minutes=0, seconds=0)]
df.loc[len(df)] = [0.894139, timedelta(hours=4, minutes=0, seconds=0)]
df.loc[len(df)] = [0.873184, timedelta(hours=5, minutes=0, seconds=0)]
df.loc[len(df)] = [0.847748, timedelta(hours=6, minutes=0, seconds=0)]
df.loc[len(df)] = [0.828572, timedelta(hours=7, minutes=0, seconds=0)]
df.loc[len(df)] = [0.807029, timedelta(hours=8, minutes=0, seconds=0)]
df.loc[len(df)] = [0.780847, timedelta(hours=9, minutes=0, seconds=0)]
df.loc[len(df)] = [0.761082, timedelta(hours=10, minutes=0, seconds=0)]
df.loc[len(df)] = [0.738855, timedelta(hours=11, minutes=0, seconds=0)]
df.loc[len(df)] = [0.711733, timedelta(hours=12, minutes=0, seconds=0)]

input = 0.80
i = datetime.now()
next_time = i + df.ix[(df['prob']-input).abs().argsort()[:1]]['time']

print(i)
print(next_time)