Given a dataset like this:
values = ([ 'motorway' ] * 5) + ([ 'link' ] * 3) + ([ 'motorway' ] * 7)
df = pd.DataFrame.from_dict({
'timestamp': pd.date_range(start='2018-1-1', end='2018-1-2', freq='s').tolist()[:len(values)],
'road_type': values,
})
df.set_index('timestamp')
df['delta_t'] = (df['timestamp'] - df['timestamp'].shift()).fillna(0)
I want to have the max sums of delta_t
per group of consecutive road_types; given that delta_t
is going to be 1s
in this sample case, I want to find motorway
: 7s
and link
: 3s
. In practice there will be more road_types, and delta_t
will vary.
edit: the solution provided here seems similar but does not sum the times nor does it select the largest of each groups.