Skip to content
Advertisement

Converting a time series into start & end dates using Pandas

I’m simply looking for a more intuitive and faster way to get start and end times of uninterrupted time sequences. Here’s a reproducible example as well as my way of doing it for the time being:

JavaScript

Resulting dataframe:

JavaScript

Any kind of help will be valuable.

Advertisement

Answer

Another approach is to create a group column indicating which group each row belongs to. This can be done by using shift and cumsum. After that, we can simply use groupby to create the wanted columns.

JavaScript

We need to remove 1 from the count value since it’s the time we want, not simply the number of rows in each group. The last two lines are simply for cleanup to match the wanted output dataframe.

Result:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement