Use multiple dates in pd.date_range


I have a range of dates in date column of dataframe. The dates are scattered eg 1st feb, 5th Feb, 11th feb etc.

I want to use pd.date_range with frequency one minute on every date in this column. So my start argument will be date and the end argument will be date + datetime.timedelta(days=1).

I’m struggling with using apply function with this, can someone help me with it? or can I use some other function over here?

I don’t want to use a for loop because the length of my dates will be HUGE.

I tried this : x : pd.date_range(start=df['date'],end = df['date']+datetime.timedelta(days=1),freq="1min"),axis =1)

but I’m getting error.

Thanks in advance

>Solution :

Use x in lambda function instead df['date'] and remove axis=1:

df = pd.DataFrame({'date':pd.date_range('2021-11-26', periods=3)})

print (df)
0 2021-11-26
1 2021-11-27
2 2021-11-28

s = df['date'].apply(lambda x:pd.date_range(start=x,end=x+pd.Timedelta(days=1),freq="1min"))
print (s)
0    DatetimeIndex(['2021-11-26 00:00:00', '2021-11...
1    DatetimeIndex(['2021-11-27 00:00:00', '2021-11...
2    DatetimeIndex(['2021-11-28 00:00:00', '2021-11...
Name: date, dtype: object.Timedelta(days=1),freq="1min"))

Leave a ReplyCancel reply