简体   繁体   中英

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

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??

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM