Analyze Apple Health Data with Python: Your First Three Charts

Sean WadeUpdated 6 min read

This walkthrough turns three Apple Health datasets into three charts: workouts per week, recorded sleep duration, and daily resting heart rate. Along the way, it handles a less photogenic part of analysis: the days with no data.

The script reads PulsHealth's product API CSV exports. The downloadable files are synthetic, so you can run every step without exporting or sharing your own records. Their values demonstrate file structure and missing-data handling, not normal health ranges.

Updated October 25, 2026: clarified the calendar rules and added input checks so blank workout timestamps, missing daily dates, and non-finite values cannot quietly produce a misleading chart. The downloadable example was rerun with its pinned dependencies.

Run the example first

Download these five files into one folder:

Use Python 3.11 or later. Open a terminal in that folder, create an environment, and install the plotting dependencies:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python analyze.py --time-zone UTC

On Windows, activate the environment with .venv\Scripts\activate instead. The script saves figures/three-charts.webp and figures/daily-summary.csv. Its default window is August 3–30, 2026, inclusive: the supplied --end date of August 31 is exclusive.

The example run reports 12 workouts, 25 days with sleep values, and 26 days with resting-heart-rate values across 28 calendar days. These counts are useful checks that you downloaded and processed the intended files.

Three charts from synthetic data: three recorded workouts in each of four weeks, sleep duration with three missing days, and resting heart rate with two missing days.
Open full-size image
Generated by the downloadable script. Every value is synthetic. Gaps remain gaps; missing sleep and heart-rate values are not replaced with zero.

Replace export.xml and keep your Python chart

Download workout_chart.py alongside the sample workouts CSV and use the environment above. This smaller example counts recorded workouts by day. Both input readers return a DataFrame with one UTC timestamp column named start. The chart code is identical for both readers.

python workout_chart.py workouts.csv --time-zone UTC --output sample.png
python workout_chart.py export.xml --time-zone Europe/London --output before.png

To switch to automatic phone sync, pair your iPhone with the stack and allow Workouts. Install puls-export and set your product API URL and read token locally. Pick the same time zone and comparison window as your chart.

puls-export --dataset workouts --start 2026-08-01 --end 2026-09-01 \
  --time-zone Europe/London -o august.csv
python workout_chart.py august.csv --time-zone Europe/London --output after.png

Compare counts on a day covered by both inputs. The XML example reads the full archive, while the CSV covers only the dates requested. After another workout syncs, rerun the export with an end date after that workout and rerun the same chart. The phone sends changes incrementally. The chart reloads a complete snapshot of its selected window, which also picks up late arrivals and deletions. Replacing the input avoids double-counting on repeated runs.

Know which export you have

Apple Health's built-in export, PulsHealth's on-device files, and the PulsHealth product API are different inputs. This script expects the product API's workouts, sleep, and daily_metrics datasets. It is not an XML parser, and renaming Apple's XML file to CSV will not make it compatible.

The export reference defines the columns. Three distinctions matter here:

DatasetWhat one row meansField used
workoutsOne recorded workoutuuid, start
sleepA derived local-day sleep summarydate, asleepMinutes
daily_metricsOne daily value for one metricidentifier, unit, date, value

Workout instants use epoch milliseconds. Sleep dates use the account's reporting calendar. Daily metrics can preserve the phone's recorded day or use the account reporting calendar, depending on their source; the export retains those assigned dates. An empty numeric CSV cell means missing data; it does not mean zero.

The daily sleep summary is a derived view, not a list of individual sleep-stage intervals. Keep its assigned date. Rebuilding a night by summing every raw sleep interval introduces additional questions about sources, overlaps, naps, and intervals crossing midnight.

Chart one: count recorded workouts

The script converts each workout start into the chosen time zone, takes its local calendar date, and counts records in Monday-starting weeks. Pandas documents the explicit unit and utc arguments used when parsing timestamps.

local = pd.to_datetime(
    workouts.start, unit="ms", utc=True
).dt.tz_convert(args.time_zone)
workout_days = local.dt.tz_localize(None).dt.normalize()

That conversion comes before grouping. Otherwise, an evening workout can land on the following UTC date and, near a week boundary, in the wrong week.

We count workout UUIDs, not minutes of elevated heart rate. A walk without a recorded workout does not enter this chart. Two apps may also record the same session using different UUIDs. The script rejects duplicate UUIDs and blank identifiers or start times, but it cannot decide whether two distinct records describe one real activity. Inspect suspiciously close start times before interpreting a sudden jump.

A bar of zero means no exported workout records in that part of the window. It does not prove there was no exercise, nor that syncing and permissions were complete. The first or last week can also be partial when you change the date range. Compare complete weeks when assessing consistency.

Chart two: preserve missing sleep

The sleep plot divides asleepMinutes by 60 and reindexes onto every date in the requested range. Reindexing introduces missing values for absent dates, so the line breaks instead of inventing a zero-hour night.

The figure includes coverage in its title: 25 of 28 days have values. That denominator should travel with any summary you write. “Average recorded sleep across 25 days” says something different from “average sleep across the month.”

Do not fill gaps by carrying yesterday's sleep forward. The purpose is to describe the recorded nights. A gap might arise from not wearing the watch, a charging interruption, missing permissions, or an incomplete export; the CSV alone does not identify the cause.

Also resist interpreting this chart as a sleep-quality score. It shows duration from the exported summary. It does not establish how restorative sleep was or diagnose a sleep condition.

Chart three: use the resting-heart-rate metric

The script selects HKQuantityTypeIdentifierRestingHeartRate and requires the canonical count/min unit. That is beats per minute. It does not calculate “resting heart rate” by averaging all ordinary heart-rate samples, which would mix workouts, movement, and rest.

The daily metric is itself a summary. PulsHealth's database guide explains that canonical HealthKit daily aggregates take precedence, with a defined rollup fallback when an aggregate is absent. That distinction matters when reconciling an exported value with another app's display.

This first chart leaves daily observations visible and unsmoothed. A rolling average could be a useful second view, but it also raises a choice about the minimum number of observations in each window. Start with the data and its gaps before adding a trend line.

Replace the example with your records

With a configured PulsHealth server and the puls-export CLI, save the corresponding files into a separate directory. Set PULS_API_URL and PULS_API_TOKEN in your local environment according to the export instructions. Keep credentials out of scripts and notebooks.

mkdir my-data
puls-export --dataset workouts --start 2026-08-03 \
  --end 2026-08-31 --time-zone Europe/London -o my-data/workouts.csv
puls-export --dataset sleep --start 2026-08-03 \
  --end 2026-08-31 --time-zone Europe/London -o my-data/sleep.csv
puls-export --dataset daily_metrics \
  --types HKQuantityTypeIdentifierRestingHeartRate \
  --start 2026-08-03 --end 2026-08-31 \
  --time-zone Europe/London -o my-data/daily_metrics.csv
python analyze.py --input my-data --output my-figures \
  --time-zone Europe/London --start 2026-08-03 --end 2026-08-31 \
  --label "Personal recorded data"

Replace Europe/London in all four commands with your account's reporting time zone (users.time_zone, falling back to PULS_TIME_ZONE). The CLI time zone interprets date bounds; it does not redefine stored daily-summary dates. Phone aggregates with temporal context retain their recorded calendar day, so travel can make those days differ from raw reporting days. Inspect the calendar rules and a boundary day before combining datasets.

Before drawing conclusions, open daily-summary.csv and inspect a few days against the original files. Check a day with a workout, a missing day, and a date near a boundary. If a dataset is empty, first investigate coverage and configuration. Do not turn an empty chart into a health conclusion.

For a real training-history example, my marathon analysis uses weekly mileage, longest runs, and recorded running days to examine one event’s buildup.

Keep the input files and command alongside your figures. That gives you enough information to rerun the analysis when the export changes, and to explain exactly what each chart counted.

apple-healthpythondata-analysis

Keep reading

The rest of the blog, or the reference pages for every health data type.