How can I generate random dates within a range of dates on bimonthly basis in numpy
? One way I can think of is generating two sets of random integer arrays:
bimonthly1 = np.random.randint(1,15,12)
bimonthly2 = np.random.randint(16,30,12)
I can then generate the dates, with the 'day' values from the above two arrays for each month. However, this will require me to explicitly pass month and year data. A solution would be to generate the desired date_range
first and substitute the 'days' in the range with the above array values. But for a large array, this may not be the best solution. This method will require operation on each and every element of the range.
I would appreciate any pointers on how to do this in numpy
more efficiently.
There is a much easier way to achieve this, without needing to explicitly call any libraries beyond numpy.
Numpy has a datetime datatype that is quite powerful: specifically for this case you can add and subtract integers and it treats it like the smallest time unit available. for example, for a %Y-%m-%d format:
exampledatetime1 = np.datetime64('2017-01-01')
exampledatetime1 + 1
>>
2017-01-02
however, for a %Y-%m-%d %H:%M:%S format:
exampledatetime2 = np.datetime64('2017-01-01 00:00:00')
exampledatetime2 + 1
>>
2017-01-01 00:00:01
in this case, as you only have information down to a day resolution, you can simply do the following:
import numpy as np
bimonthly_days = np.arange(0, 60)
base_date = np.datetime64('2017-01-01')
random_date = base_date + np.random.choice(bimonthly_days)
or if you wanted to be even cleaner about it:
import numpy as np
def random_date_generator(start_date, range_in_days):
days_to_add = np.arange(0, range_in_days)
random_date = np.datetime64(start_date) + np.random.choice(days_to_add)
return random_date
and then just use:
yourdate = random_date_generator('2012-01-15', 60)
You could create the date range a priori, e.g. using pandas
's date_range
, and convert it to a numpy array. Then, make random choices from this array of dates using numpy.random.choice
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With