There is a much simpler way to achieve this, without having to explicitly access libraries outside of numpy.
Numpy has a datetime data type that is powerful enough: specifically for this case, you can add and subtract integers, and it treats it as the smallest unit of time available. for example, for the format% Y-% m-% d:
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, since you only have information before the resolution of the day, 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 want to be cleaner:
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)
source share