Python 3 - I need to create a new df with ceil and floor for each system

Viewed 35

everyone.

So I have a data frame that cointains every failure described per system, failure event, start time and end time. I need to round the start time to the lowest ten minute and the end time to the upper ten minute.

For example:

system   event             start                end
A0201    No communication  2021-01-01 00:03:20  2021-01-01 01:36:01
A0202    Turbine Pause     2021-01-01 11:47:23  2021-01-01 11:49:43
A0201    Acelerometer Vib  2021-01-02 16:47:30  2021-01-02 16:53:51

What I need as an output is:

system   event             start                end
A0201    No communication  2021-01-01 00:00:00  2021-01-01 01:40:00
A0202    Turbine Pause     2021-01-01 11:40:00  2021-01-01 11:50:00
A0201    Acelerometer Vib  2021-01-02 16:40:00  2021-01-02 17:00:00

This is just 3 rows of my dataframe. My df has more than 10.000 lines with 49 different systems and 100+ failure events

I thought of the ceil() and floor() functions, but I'm having a dificult time writing the for loop. Can anyone help me?

Thanks!

1 Answers

Assuming your start and end columns are already of type datetime, you can use .dt.floor and .dt.ceil with 10min as frequency:

df.start = df.start.dt.floor('10min')
df.end = df.end.dt.ceil('10min')

df
#  system             event               start                 end
#0  A0201  No communication 2021-01-01 00:00:00 2021-01-01 01:40:00
#1  A0202     Turbine Pause 2021-01-01 11:40:00 2021-01-01 11:50:00
#2  A0201  Acelerometer Vib 2021-01-02 16:40:00 2021-01-02 17:00:00
Related