Home > Software engineering >  How to set a random time in a datetime
How to set a random time in a datetime

Time:04-12

My instructions are as follows: Read the date columns in as timestamps, convert them to YYYY/MM/DD hours:minutes:seconds format, where you set hours minutes and seconds to random values appropriate to their range

Here is column of the data frame we are suppose to alter to datetime:

Order date
11/12/2016
11/24/2016
6/12/2016
10/12/2016
...

And here is the date time I need

2016/11/12 (random) hours:minutes:seconds
2016/11/24 (random) hours:minutes:seconds
...

My main question is how do I get random hours minutes and seconds. The rest I can figure out with the documentation

CodePudding user response:

You can generate random numbers between 0 and 86399 (number of seconds in a day - 1) and convert to a TimeDelta with pandas.to_timedelta:

import numpy as np
time = pd.to_timedelta(np.random.randint(0, 60*60*24-1, size=len(df)), unit='s')

df['Order date'] = pd.to_datetime(df['Order date']).add(time)

Output:

           Order date
0 2016-11-12 02:21:53
1 2016-11-24 13:26:00
2 2016-06-12 15:13:03
3 2016-10-12 14:45:12

CodePudding user response:

You're trying to read the data in '%Y-%m-%d' format but the data is in "%d/%m/%Y" format. See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior to find out how to convert the date to your desired format.

  • Related