I have the following dataframe and can calculate the splits between the Startand End timestamp. It doesn't work for periods longer than 1 day and i can't concact my df properly:
import pandas as pd
import datetime as dt
df = pd.DataFrame({'Start':['2022-06-07 06:24:48','2022-06-07 14:37:16','2022-06-07 08:00:59'],
'End':['2022-06-07 14:07:00','2022-06-08 02:51:21','2022-06-09 13:18:34'],
'Process':['PROD','VORG','STO'],
'Duration_Min':[462.20,734.08,3197.58]})
df['Start'] = pd.to_datetime(df['Start'])
df['End'] = pd.to_datetime(df['End'])
#Calculate the difference in days
df['difference']=df['End'].dt.date-df['Start'].dt.date
splits = df[df.End.dt.date > df.Start.dt.date].copy()
print(pd.concat([
df,
pd.DataFrame({
'Start': list(splits.Start) + list(splits.End.dt.floor(freq='1D')),
'End': list(splits.Start.dt.ceil(freq='1D')) + list(splits.End)})
]))
What I get:
Start End Process Duration_Min difference
0 2022-06-07 06:24:48 2022-06-07 14:07:00 PROD 462.20 0 days
1 2022-06-07 14:37:16 2022-06-08 02:51:21 VORG 734.08 1 days
2 2022-06-07 08:00:59 2022-06-09 13:18:34 STO 3197.58 2 days
0 2022-06-07 14:37:16 2022-06-08 00:00:00 NaN NaN NaT
1 2022-06-07 08:00:59 2022-06-08 00:00:00 NaN NaN NaT
2 2022-06-08 00:00:00 2022-06-08 02:51:21 NaN NaN NaT
3 2022-06-09 00:00:00 2022-06-09 13:18:34 NaN NaN NaT
I would like to cut the events so that new timestamps with new intervals are created when the day changes. Days should be corosponding with weekday()
What I want:
Start End Process Duration_Min Days
0 2022-06-07 06:24:48 2022-06-07 14:07:00 PROD 462.200000 1
1 2022-06-07 14:37:16 2022-06-07 23:59:59 VORG 562.716667 1
2 2022-06-08 00:00:00 2022-06-08 02:51:21 VORG 171.350000 2
3 2022-06-07 08:00:59 2022-06-07 23:59:59 STO 959.000000 1
4 2022-06-08 00:00:00 2022-06-08 23:59:59 STO 1439.983333 2
5 2022-06-09 00:00:00 2022-06-09 13:18:34 STO 798.566667 3