Search code examples
pandasdatetimegroup-bytimedelta

Add 10 to 40 minutes randomly to a datetime column in pandas


I have a data frame as shown below

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

To the above df, I would like to add a column called 'finish' by adding minutes between 10 to 40 with start column randomly with replacement.

Expected Ouput:

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

Solution

  • Create timedeltas by to_timedelta and numpy.random.randint for integers between 10 and 40:

    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