I am working on a data frame df
with the following structure:
start_time end_time key vol
0 2018-08-23 00:00:00 2018-08-23 01:30:00 abcd_eg 0.92
1 2018-08-23 00:15:00 2018-08-23 01:45:00 defg_x2 0.27
I am trying to generate a 15 minute interval between a start and an end time. I would like to have to result as new rows in the same dataframe (or a new dataframe) as follows:
start_time end_time key vol
0 2018-08-23 00:00:00 2018-08-23 01:30:00 abcd_eg 0.92
1 2018-08-23 00:15:00 2018-08-23 01:30:00 abcd_eg 0.92
2 2018-08-23 00:30:00 2018-08-23 01:30:00 abcd_eg 0.92
3 2018-08-23 00:45:00 2018-08-23 01:30:00 abcd_eg 0.92
4 2018-08-23 01:00:00 2018-08-23 01:30:00 abcd_eg 0.92
5 2018-08-23 01:15:00 2018-08-23 01:30:00 abcd_eg 0.92
6 2018-08-23 01:30:00 2018-08-23 01:30:00 abcd_eg 0.92
7 2018-08-23 00:15:00 2018-08-23 01:45:00 defg_x2 0.27
8 2018-08-23 00:30:00 2018-08-23 01:45:00 defg_x2 0.27
9 2018-08-23 00:45:00 2018-08-23 01:45:00 defg_x2 0.27
10 2018-08-23 01:00:00 2018-08-23 01:45:00 defg_x2 0.27
11 2018-08-23 01:15:00 2018-08-23 01:45:00 defg_x2 0.27
12 2018-08-23 01:30:00 2018-08-23 01:45:00 defg_x2 0.27
13 2018-08-23 01:45:00 2018-08-23 01:45:00 defg_x2 0.27
The date columns are of type datetime[64]
, key is object
and vol is float
.
What I tried till now is:
b=[]
lst = []
for i, row in df.iterrows():
b = pd.date_range(start=row.start_time, end=row.end_time, freq='15min',closed=None)
lst.append(b)
Used .iterrows()
as I have about 125 records. This gives me the timeseries as DatetimeIndex
for all the values of start and the end time in the dataframe, with 15 minute intervals.
After this, I tried to pass lst
as a new column unpack
in the dataframe df
as follows:
df['unpack'] = lst
My thoughts were that if I could get these values as the new column in df, I could use this solution to extract them as rows. But this process is not working.
How can I do this with pandas?