Home > OS >  How to create date range with multiple start and end dates?
How to create date range with multiple start and end dates?

Time:08-25

I have a dataframe from which I want to create a date range so that I can use this in another df to exclude the dates from it.

This is the df from which I want to create a date range.

    name        start_date  end_date
0   range_1     2022-04-11  2022-04-24
1   range_2     2022-07-25  2022-09-03
2   range_3     2022-10-24  2022-10-30
3   range_4     2022-12-22  2023-01-08

How can I do this within one variable??

CodePudding user response:

You can use .apply if you really need the result to be within one variable:

df[['start_date', 'end_date']].apply(
    lambda row: pd.date_range(row['start_date'], row['end_date']), 
    axis=1
)

This will return a Series that contains DatetimeIndex-es:

0    DatetimeIndex(['2022-04-11', '2022-04-12', '20...
1    DatetimeIndex(['2022-07-25', '2022-07-26', '20...
2    DatetimeIndex(['2022-10-24', '2022-10-25', '20...
3    DatetimeIndex(['2022-12-22', '2022-12-23', '20...
dtype: object
  • Related