-1

I have df containing randomly created dates in the current year.

df['timestamp_open'] = [randomtimestamp(start_year=2023,end_year=2023) for _ in range(len(df.index))]

I would need additional datetime column whose values are randomly higher, but not more than 5 days. Don't know how to do it

Edit: adding a fragment of df containing randomly created datetime, as per comment:

df fragment

  • post a fragment of *df containing randomly created dates* – RomanPerekhrest Feb 20 '23 at 17:57
  • Does this answer your question? [Generate random integers between 0 and 9](https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9) then use [this](https://stackoverflow.com/q/16385785/843953) to add that number of days to your column – Pranav Hosangadi Feb 20 '23 at 18:08

1 Answers1

1

You can add random amount of minutes (or seconds, whatever you'd like) using TimeDelta. Consider the following example:

from random import randint
from pandas import Timedelta

df = pd.DataFrame({"date": pd.date_range("2023-01-01", "2023-10-01", 10)})
df['rand_add_date'] = df.apply(lambda r: r['date'] + Timedelta(randint(1,5*24*60), unit='minutes'), axis=1)
print(df)
#                  date       rand_add_date
# 0 2023-01-01 00:00:00 2023-01-01 14:33:00
# 1 2023-01-31 08:00:00 2023-02-02 16:08:00
# 2 2023-03-02 16:00:00 2023-03-05 10:42:00
# 3 2023-04-02 00:00:00 2023-04-03 00:53:00
# 4 2023-05-02 08:00:00 2023-05-05 12:29:00
# 5 2023-06-01 16:00:00 2023-06-04 14:51:00
# 6 2023-07-02 00:00:00 2023-07-02 23:58:00
# 7 2023-08-01 08:00:00 2023-08-03 15:45:00
# 8 2023-08-31 16:00:00 2023-09-04 03:55:00
# 9 2023-10-01 00:00:00 2023-10-01 03:41:00
JarroVGIT
  • 4,291
  • 1
  • 17
  • 29