1

I'm trying to generate multiple random datetime stamps between two dates,

I tried with using the below code based on the existing post and question, but it generates only a single random date time.


import datetime
import random
import pandas as pd

min_date = pd.to_datetime('2019-01-01 00:00:00')
max_date = pd.to_datetime('2019-01-01 23:59:59')

start + datetime.timedelta(seconds=random.randint(0, int((end - start).total_seconds())),)

>>> Timestamp('2019-09-27 05:58:40')

Is there a way to generate multiple date time based on the size mentioned. Say if the size is mentioned as 100, it should generate 100 random date timestamps objects similar to the output as mentioned above. Also I want to store the 100 time stamps in a pandas dataframe.

user3046211
  • 466
  • 2
  • 13

2 Answers2

0

Try this:

import datetime
import random
import pandas as pd

min_date = pd.to_datetime('2019-01-01 00:00:00')
max_date = pd.to_datetime('2019-01-01 23:59:59')
for x in range(100):
    print(start + datetime.timedelta(seconds=random.randint(0, int((end - start).total_seconds())),))

It will generate 100 random timestamps

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0
N       = 100
diff    = (max_date - min_date).total_seconds() + 1
offsets = random.sample(range(int(diff)), k=N)
result  = min_date + pd.TimedeltaIndex(offsets, unit="s")
  • get the difference between start & end in seconds
    • add 1 because range used next is end-exclusive
  • sample N seconds from 0 to diff, and convert it to TimedeltaIndex for vectorized addability
  • add those offsets to the starting date

Example run:

In [60]: N = 10
    ...: diff = (max_date - min_date).total_seconds() + 1
    ...: offsets = random.sample(range(int(diff)), k=N)
    ...: result = min_date + pd.TimedeltaIndex(offsets, unit="s")

In [61]: result
Out[61]:
DatetimeIndex(['2019-01-01 16:30:47', '2019-01-01 00:05:32',
               '2019-01-01 02:35:15', '2019-01-01 21:25:09',
               '2019-01-01 19:09:26', '2019-01-01 06:25:37',
               '2019-01-01 07:28:47', '2019-01-01 00:25:18',
               '2019-01-01 17:04:58', '2019-01-01 05:15:46'],
              dtype='datetime64[ns]', freq=None)

what's returned is a DatetimeIndex but .tolist()ing it would give a list of Timestamps if so desired.

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38