Search code examples
pythonpandasdatedate-range

Creating a single column of dates from a column of start dates and a column of end dates - python


I'd like to create a single column of dates using two columns of dates, one column is the start dates and the other column is the end dates. The time increment I'm looking for is 30 min. So for example if I have these two columns of dates:

Start End
2019-10-15 15:30 2019-10-15 16:30
2019-10-17 11:00 2019-10-17 12:30
2019-11-01 03:30 2019-11-01 04:00

I'd like to get:

2019-10-15 15:30
2019-10-15 16:00
2019-10-15 16:30
2019-10-17 11:00
2019-10-17 11:30
2019-10-17 12:00
2019-10-17 12:30
2019-11-01 03:30
2019-11-01 04:00

I've tried many things with pd.date_range, numpy arange, etc., but haven't been able to figure out how to make a single column of data. I'm new to python.


Solution

  • >>> df.apply(lambda dt: pd.date_range(dt["Start"], dt["End"], freq="30T"), 
                 axis="columns").explode(ignore_index=True)
    
    0   2019-10-15 15:30:00
    1   2019-10-15 16:00:00
    2   2019-10-15 16:30:00
    3   2019-10-17 11:00:00
    4   2019-10-17 11:30:00
    5   2019-10-17 12:00:00
    6   2019-10-17 12:30:00
    7   2019-11-01 03:30:00
    8   2019-11-01 04:00:00
    dtype: datetime64[ns]