将10到40分钟随机添加到熊猫的日期时间列

时间:2020-04-08 11:12:26

标签: pandas pandas-groupby

我有一个如下所示的数据框

start                      
2010-01-06 09:00:00
2018-01-07 08:00:00
2012-01-08 11:00:00
2016-01-07 08:00:00
2010-02-06 14:00:00
2018-01-07 16:00:00

在上述df中,我想添加一个名为“完成”的列,方法是在10到40之间添加分钟,并在开始列中随机替换。

预期的输出量:

start                       finish
2010-01-06 09:00:00      2010-01-06 09:20:00
2018-01-07 08:00:00      2018-01-07 08:12:00
2012-01-08 11:00:00      2012-01-08 11:38:00
2016-01-07 08:00:00      2016-01-07 08:15:00
2010-02-06 14:00:00      2010-02-06 14:24:00
2018-01-07 16:00:00      2018-01-07 16:36:00

2 个答案:

答案 0 :(得分:1)

to_timedeltanumpy.random.randint1040之间的整数创建时间增量:

arr = np.random.randint(10, 40, size=len(df))
df['finish'] = df['start'] + pd.to_timedelta(arr, unit='Min')
print (df)
                start              finish
0 2010-01-06 09:00:00 2010-01-06 09:25:00
1 2018-01-07 08:00:00 2018-01-07 08:30:00
2 2012-01-08 11:00:00 2012-01-08 11:29:00
3 2016-01-07 08:00:00 2016-01-07 08:12:00
4 2010-02-06 14:00:00 2010-02-06 14:31:00
5 2018-01-07 16:00:00 2018-01-07 16:39:00

答案 1 :(得分:0)

您可以通过结合使用pandas.Series.apply()pandas.to_timedelta()random.randint()来实现。

from random import randint
df['finish'] = df.start.apply(lambda dt: dt + pd.to_timedelta(randint(10, 40), unit='m'))