Skip to content

Fetching 3 Months of Cycling Data

This tutorial demonstrates how to load and analyze the timeseries data from multiple cycling activities over a 3-month period using SweatStack.

Understanding Longitudinal Data

When analyzing fitness trends, it's valuable to look at timeseries data across multiple activities rather than just individual workouts. In SweatStack, this multi-activity data is called longitudinal data.

In this tutorial, we'll show you how to fetch longitudinal data for cycling and perform basic analysis on this aggregated data.

Quick Example

Here's a complete example showing how to fetch and analyze your cycling data from the last 3 months:

longitudinal_data.py
# /// script
# dependencies = [
#   "sweatstack",
# ]
# ///

import sweatstack as ss
import pandas as pd
from datetime import date, timedelta


ss.login()


longitudinal_data = ss.get_longitudinal_data(
    sports=["cycling"],
    start=date.today() - timedelta(days=90),
)

print(f"Found {longitudinal_data['activity_id'].nunique()} cycling activities in the last 3 months")

total_duration = longitudinal_data["duration"].sum()
mean_speed = longitudinal_data["speed"].mean()

print(f"Total time: {total_duration}")
print(f"Average speed: {mean_speed} m/s")

Save this file and run it in your terminal with:

uv run longitudinal_data.py

The returned dataframe contains the second-by-second data of all your cycling activities in the selected time period.

By default, the dataframe contains the following columns: duration, power, heart_rate and speed, but any metric can be requested by providing the metrics parameter:

cycling_activities = ss.get_longitudinal_data(
    sports=["cycling"],
    start=date.today() - timedelta(days=90),
    metrics=["power", "cadence", "core_temperature"],
)

Calculate weekly max power

As an example, let's calculate the weekly max power:

weekly_max_power = longitudinal_data["power"].resample("W").max()

print(weekly_max_power)

This will return a pandas Series with the max power for each week.

Activity Metadata

When you are not interested in the timeseries data, but want to look at trends in the activity metadata, you can use the get_activities() function:

activities = ss.get_activities(
    sports=["cycling"],
    start=date.today() - timedelta(days=90),
    as_dataframe=True,  # This will return a pandas DataFrame instead of a list of Activity objects
)