Home > OS >  Split one row into multiple rows of 6 hours data based on 15 mins time interval in pandas data frame
Split one row into multiple rows of 6 hours data based on 15 mins time interval in pandas data frame

Time:11-26

I want Split one row into multiple rows of 6 hours data based on 15 mins time interval in pandas data frame

    start_time          end_time    
0   2022-08-22 00:15:00 2022-08-22 06:15:00

I have tried one hrs time split and used below code

result['start_time'] = result.apply(lambda d: pd.date_range(d['start_time'],
                                                    d['end_time'], 
                                                    freq='h')[:-1], 
                            axis=1) 

and it worked for me to get this

   result["start_time"][0]

Output:

DatetimeIndex(['2022-08-22 00:15:00', '2022-08-22 01:15:00',
               '2022-08-22 02:15:00', '2022-08-22 03:15:00',
               '2022-08-22 04:15:00', '2022-08-22 05:15:00'],
              dtype='datetime64[ns]', freq='H')

now i want the frequency for 15 mins time interval, so it should give me 24 timestamp

CodePudding user response:

from datetime import timedelta

df = pd.DataFrame({'start_time': ['2022-08-22 00:15:00'],'end_time': ['2022-08-22 06:15:00']})
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])
df['start_time'] = df['start_time'].dt.strftime('%Y-%m-%d %H:%M:%S')
df['end_time'] = df['end_time'].dt.strftime('%Y-%m-%d %H:%M:%S')

#             start_time             end_time
# 0  2022-08-22 00:15:00  2022-08-22 06:15:00

new_df = pd.date_range(start=df['start_time'][0], end=df['end_time'][0], freq='15min')
result_df = pd.DataFrame({'start_time': new_df, 'end_time': new_df   timedelta(minutes=15)})

output:

> result_df

      start_time            end_time
0  2022-08-22 00:15:00 2022-08-22 00:30:00
1  2022-08-22 00:30:00 2022-08-22 00:45:00
2  2022-08-22 00:45:00 2022-08-22 01:00:00
3  2022-08-22 01:00:00 2022-08-22 01:15:00
4  2022-08-22 01:15:00 2022-08-22 01:30:00
5  2022-08-22 01:30:00 2022-08-22 01:45:00
6  2022-08-22 01:45:00 2022-08-22 02:00:00
7  2022-08-22 02:00:00 2022-08-22 02:15:00
8  2022-08-22 02:15:00 2022-08-22 02:30:00
9  2022-08-22 02:30:00 2022-08-22 02:45:00
10 2022-08-22 02:45:00 2022-08-22 03:00:00
11 2022-08-22 03:00:00 2022-08-22 03:15:00
12 2022-08-22 03:15:00 2022-08-22 03:30:00
13 2022-08-22 03:30:00 2022-08-22 03:45:00
14 2022-08-22 03:45:00 2022-08-22 04:00:00
15 2022-08-22 04:00:00 2022-08-22 04:15:00
16 2022-08-22 04:15:00 2022-08-22 04:30:00
17 2022-08-22 04:30:00 2022-08-22 04:45:00
18 2022-08-22 04:45:00 2022-08-22 05:00:00
19 2022-08-22 05:00:00 2022-08-22 05:15:00
20 2022-08-22 05:15:00 2022-08-22 05:30:00
21 2022-08-22 05:30:00 2022-08-22 05:45:00
22 2022-08-22 05:45:00 2022-08-22 06:00:00
23 2022-08-22 06:00:00 2022-08-22 06:15:00
24 2022-08-22 06:15:00 2022-08-22 06:30:00

CodePudding user response:

Try: 15T instead of h

result['start_time'] = result.apply(lambda d: pd.date_range(d['start_time'],
                                                    d['end_time'], 
                                                    freq='15T')[:-1], 
                            axis=1) 

OUTPUT:

DatetimeIndex(['2022-08-22 00:15:00', '2022-08-22 00:30:00',
               '2022-08-22 00:45:00', '2022-08-22 01:00:00',
               '2022-08-22 01:15:00', '2022-08-22 01:30:00',
               '2022-08-22 01:45:00', '2022-08-22 02:00:00',
               '2022-08-22 02:15:00', '2022-08-22 02:30:00',
               '2022-08-22 02:45:00', '2022-08-22 03:00:00',
               '2022-08-22 03:15:00', '2022-08-22 03:30:00',
               '2022-08-22 03:45:00', '2022-08-22 04:00:00',
               '2022-08-22 04:15:00', '2022-08-22 04:30:00',
               '2022-08-22 04:45:00', '2022-08-22 05:00:00',
               '2022-08-22 05:15:00', '2022-08-22 05:30:00',
               '2022-08-22 05:45:00', '2022-08-22 06:00:00'],
              dtype='datetime64[ns]', freq='15T')

As expected - you get your 24 timestamps

  • Related