Home > Software design >  Choosing a date randomly in a period?
Choosing a date randomly in a period?

Time:07-20

I want to randomly choose a date from 2021/1/1 to 2021/12/31, the process might include as follows:

  1. generate a date list from 2021/1/1 to 2021/12/31, totally 365 elements;
  2. randomly choose a date from the list.

Thanks!

CodePudding user response:

As you tagged the question , here is a pandas way:

out = (pd.date_range('2021/1/1', '2021/12/31')  # random dates
         .strftime('%Y/%m/%d')                  # format as string
         .to_series().sample(n=1)               # select 1 random date
         .squeeze()                             # compress output
      )

variant, setting the start date and number of days

out = (pd.date_range('2021/1/1', periods=365)   # random dates
         .strftime('%Y/%m/%d')                  # format as string
         .to_series().sample(n=1)               # select 1 random date
         .squeeze()                             # compress output
      )

example output: '2021/10/09'

list of random dates

You can easily adapt to generate several dates:

out = (pd
   .date_range('2021/1/1', periods=365)
   .strftime('%Y/%m/%d').to_series()
   .sample(n=10)
   .to_list()
 )

example output:

['2021/04/06', '2021/09/11', '2021/08/02', '2021/09/17', '2021/12/30',
 '2021/10/27', '2021/03/09', '2021/02/27', '2021/11/28', '2021/01/18']

CodePudding user response:

import datetime
from datetime import date, timedelta
from random import sample

start = date(2021, 1, 1)
end = date(2021, 12, 31)

dates = []
day = start

while day <= end:
    dates.append(day)
    day = day   datetime.timedelta(days=1)

sample(dates, 1)

CodePudding user response:

Here's another way, using random between to epoch dates:

import pandas as pd
import numpy as np

date1 = pd.Timestamp("2021/01/01")
date2 = pd.Timestamp("2021/12/31")

print(date1.timestamp())

print(date2.timestamp())

n = 3  # How many samples to take
out = pd.to_datetime(
    np.random.randint(date1.timestamp(), date2.timestamp(), n), unit="s"
).normalize()

print(out)

Output:

1609459200.0
1640908800.0
DatetimeIndex(['2021-04-13', '2021-01-17', '2021-08-24'], dtype='datetime64[ns]', freq=None)
  • Related