Home > Back-end >  python interval training analysis, counting rows based on heart rate and power
python interval training analysis, counting rows based on heart rate and power

Time:08-10

currently I am working on a dataset of an athletes running interval training. based on power values I want to automatically count the number of interval sessions in a training. so if power is above 200 watts I want to count the number of seconds of that high intensity period.

code below displays an example, numbers are not realistic

df = pd.DataFrame(
    [[Timestamp('2022-08-05 10:11:04'), 140, 120],
    [Timestamp('2022-08-05 10:11:05'), 160, 155],
    [Timestamp('2022-08-05 10:11:06'), 230, 156],
    [Timestamp('2022-08-05 10:11:07'), 230, 155],
    [Timestamp('2022-08-05 10:11:08'), 230, 160],
    [Timestamp('2022-08-05 10:11:09'), 140, 130],
    [Timestamp('2022-08-05 10:11:10'), 140, 131],
    [Timestamp('2022-08-05 10:11:11'), 230, 170]],
    columns=['timestamp', 'power', 'heart rate'])

so in the end I want to know how many seconds an athlete has been in the high power zone above 200 watts

CodePudding user response:

IIUC, you can compute the diff between successive timestamps, then filter using boolean indexing and sum:

(-df['timestamp'].diff(-1))[df['power'].gt(200)].sum()

output:

Timedelta('0 days 00:00:03')

NB. this is ignoring the last row as we don't know how long the last interval lasts. If you want to consider that the interval is the same as the previous one:

(-df['timestamp'].diff(-1).ffill())[df['power'].gt(200)].sum()

output:

Timedelta('0 days 00:00:04')

per stretch of continuous values above 200:

m = df['power'].gt(200)

(-df['timestamp'].diff(-1))[m].groupby((~m).cumsum()).sum()

output:

power
2   0 days 00:00:03
4   0 days 00:00:00
Name: timestamp, dtype: timedelta64[ns]
  • Related