我需要创建包含时间值(hh:mm:ss)的数组,从每个数组中采样一个值并将其求和。我使用pd.timedelta_range()创建了数组,并使用rd.choices()进行了采样。我需要对采样的出发时间和持续时间求和,以得出到达时间,但出发时间+持续时间只是打印出两个值而未提供总计。
departure_time = pd.timedelta_range('04:00:00', '09:45:00', freq='15T')
departure_prob = (0.008, 0.008, 0.008, 0.008, 0.032, 0.032, 0.032, 0.032, 0.082, 0.082, 0.082,
0.082, 0.08, 0.08, 0.08, 0.08, 0.034, 0.034, 0.034, 0.034, 0.014, 0.014, 0.014, 0.014)
duration_time = pd.timedelta_range('00:15:00', '01:15:00', freq='15T')
duration_prob = (0.355, 0.321, 0.152, 0.086, 0.086)
departure = rd.choices(departure_time, departure_prob)
duration = rd.choices(duration_time, duration_prob)
print(departure+duration)
有没有办法做到这一点?
尝试一下
import pandas as pd
import random as rd
import datetime
#departure_time = pd.timedelta_range('04:00:00', '09:45:00', freq='15T')
departure_time = pd.date_range(start='2017-01-01 10:00', end='2017-01-02 09:00', freq='1h')
departure_prob = (0.008, 0.008, 0.008, 0.008, 0.032, 0.032, 0.032, 0.032, 0.082, 0.082, 0.082,
0.082, 0.08, 0.08, 0.08, 0.08, 0.034, 0.034, 0.034, 0.034, 0.014, 0.014, 0.014, 0.014)
duration_time = pd.timedelta_range('00:15:00', '01:15:00', freq='15T')
duration_prob = (0.355, 0.321, 0.152, 0.086, 0.086)
departure = rd.choices(departure_time, departure_prob)
duration = rd.choices(duration_time, duration_prob)
print(departure[0]+duration[0])
如果要抽时间,则必须使用日期。顾名思义,Timedelta是一个时间段。