Metrics
When you import Sweatpy with import sweat
all pandas data frames and series have a sweat
accessor available that allows you to do things like:
df.sweat.mean_max("column name")
...or
df["power"].sweat.mean_max()
See below for more examples.
The sweat accessor will raise an AttributeError
when the data frame or series content is not valid.
Please note that the sweat accessor for data frames and series are similar but not identical.
Most methods on the sweat accessor are also available for usage outside data frames.
Mean max¶
import sweat
example = sweat.examples(path="4078723797.fit")
data = sweat.read_fit(example.path)
mmp = data["power"].sweat.mean_max()
mmp
Or for multiple columns at once:
import sweat
example = sweat.examples(path="4078723797.fit")
data = sweat.read_fit(example.path)
mean_max = data.sweat.mean_max(["power", "heartrate"])
mean_max
The mean_max()
methods accept a monotic
boolean argument that can be used to force a monotonically decreasing mean max curve. The default is False
.
import sweat
example = sweat.examples(path="4078723797.fit")
data = sweat.read_fit(example.path)
mean_max = data.sweat.mean_max(["power", "heartrate"], monotonic=True)
The mean_max()
function is also available as sweat.metrics.core.mean_max()
.
Timedelta index¶
The data frames returned by the read_*()
functions have a pandas.DatetimeIndex by default.
Sometimes it is usefull to have a relative pandas.TimedeltaIndex:
import sweat
example = sweat.examples(path="4078723797.fit")
activity = sweat.read_fit(example.path)[["power", "heartrate"]]
activity = activity.sweat.to_timedelta_index()
activity.head()
Then you can do things like slice for the first 10 seconds of an activity:
activity[:"00:00:10"]
The sweat.to_timedelta_index()
method is available on both data frames and series.
Training zones¶
Working with training zones is easy in Sweatpy. To add a column with the heart rate zone label to the data frame:
import sweat
example = sweat.examples(path="4078723797.fit")
activity = sweat.read_fit(example.path)[["power", "heartrate"]]
activity["heartrate_zone"] = activity["heartrate"].sweat.calculate_zones(
bins=[0, 100, 140, 160, 999],
labels=["rest", "D1", "D2", "D3"])
activity["heartrate_zone"].head()
...where the bins
argument contains the left and right bounds for each training zone and the labels
argument the zone labels.
To calculate the time in zone:
import sweat
example = sweat.examples(path="4078723797.fit")
activity = sweat.read_fit(example.path)[["power", "heartrate"]]
time_in_zone = activity["power"].sweat.time_in_zone(
bins=[-9999, 150, 230, 320, 9999],
labels=["rest", "D1", "D2", "D3"])
time_in_zone