Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I tried this :

df.date.apply(lamda 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)
        date
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"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading