Reputation: 117
I want to generate random dates in the same format as the input, but no matter how many times I run the code, I want it to return the same random date, against each input value.
I'm reading from csv file to a data frame as follows:
in_df = pd.read_csv('file.txt',sep="\t", encoding='ISO-8859-1',usecols=['MY_DATE'])
Input dataframe:
MY_DATE
0 2015-11-30
1 2016-10-14
2 2015-11-20
Expected output after 1st call to function:
MY_DATE
0 2018-10-22
1 2019-06-15
2 2007-09-01
Expected output after 2nd call to function, for the same input:
MY_DATE
0 2018-10-22
1 2019-06-15
2 2007-09-01
2015-11-30
results in, say, 2018-10-22
on first run, but doesn't result in the same date when I rerun it, even after setting the prop as global.
Here's my code snippet:
global prop
prop= random.random()
def gen_exp_dt(prop,start_dttm, end_dttm, format=None):
stime = time.mktime(time.strptime(start_dttm,format))
etime = time.mktime(time.strptime(end_dttm, format))
rtime = stime + (prop * (etime - stime))
return time.strftime(format, time.localtime(rtime))
Any suggestions?
Upvotes: 1
Views: 15676
Reputation: 51165
Use each date as your random seed. That way, each time you apply the function, you will get the same result for each date.
Next, generate a random integer between whatever range you want (I chose 1 and time.time()
), and convert to a string with your desired format:
def random_date(seed):
random.seed(seed)
d = random.randint(1, int(time.time()))
return datetime.fromtimestamp(d).strftime('%Y-%m-%d')
This will produce consistent results:
df
MY_DATE
0 2015-11-30
1 2016-10-14
2 2015-11-20
df.MY_DATE.apply(random_date)
0 1978-07-06
1 1971-03-30
2 1998-06-05
Name: MY_DATE, dtype: object
df.MY_DATE.apply(random_date)
0 1978-07-06
1 1971-03-30
2 1998-06-05
Name: MY_DATE, dtype: object
Upvotes: 11