When PM4Py blames the wrong bottleneck
One missing column makes the library count work as waiting without raising an error; 16 rows of data are enough to expose the mistake and fix it.
In brief
- Without a start_timestamp column, the gap between two events also includes the processing time of the later step.
- Mean is the default measure when drawing; one case that spans a weekend can change which step ranks first.
- Once you have found the longest-waiting edge, you still need to test five groups of causes with the client.
PM4Py does not require a start time for each step. When the data has no start_timestamp column, the performance code copies the timestamp column in its place, and the gap between two events absorbs the processing time of the later step.
A step that takes four hours of work can therefore show up as four hours of waiting. An FDE who carries that number into a meeting will recommend adding staff where there is no bottleneck.
This exercise helps you catch the error before the client does. You will build a script that reads an event log, ranks edges by waiting time and exports an SVG. The data is just 16 simulated rows, small enough to check every result by hand.
Step 1: Install PM4Py, and choose Python 3.12
The PyPI page for version 2.7.23.15 lists Python 3.11 and later, while the official installation page asks for 3.12 or later. Python 3.12 satisfies both. You also need Graphviz, which PM4Py uses to draw models.
python3 --version
python3 -m venv .venv
source .venv/bin/activate
pip install pm4py
dot -V
These commands are for macOS and Linux. Check: the first must print 3.12 or later, and the last must print a Graphviz version. If dot does not run, install Graphviz before you reach the drawing step.
Step 2: Create a log whose answer you already know
Save the following as log.csv. Each request passes through three steps: Receive, Check and Approve.
request_id,step,started_at,finished_at
R1,Receive,2026-11-02 07:00,2026-11-02 07:30
R1,Check,2026-11-02 08:30,2026-11-02 12:30
R1,Approve,2026-11-02 14:30,2026-11-02 15:00
R2,Receive,2026-11-03 07:00,2026-11-03 07:30
R2,Check,2026-11-03 08:30,2026-11-03 12:30
R2,Approve,2026-11-03 14:30,2026-11-03 15:00
R3,Receive,2026-11-04 07:00,2026-11-04 07:30
R3,Check,2026-11-04 08:30,2026-11-04 12:30
R3,Approve,2026-11-04 14:30,2026-11-04 15:00
R4,Receive,2026-11-05 07:00,2026-11-05 07:30
R4,Check,2026-11-05 08:30,2026-11-05 12:30
R4,Approve,2026-11-05 14:30,2026-11-05 15:00
R5,Receive,2026-11-06 16:00,2026-11-06 16:30
R5,Check,2026-11-09 08:30,2026-11-09 12:30
R5,Approve,2026-11-09 14:30,2026-11-09 15:00
R6,Receive,2026-11-10 08:00,
Work out the answer by hand before writing any code. In the first four requests, the case waits 1 hour for Check and 2 hours for Approve. R5 finishes Receive at 16:30 on Friday and Check does not begin until 8:30 on Monday, which is 64 hours by the clock on the wall.
R6 has no finish time. That faulty row is planted to test the next step.
Step 3: Count the rows that disappear
format_dataframe identifies the case ID, activity and timestamp columns, converts data types and sorts the events. It also drops any row missing one of those three values. Create a file called wait.py:
import pandas as pd
import pm4py
df = pd.read_csv("log.csv")
for col in ["started_at", "finished_at"]:
df[col] = pd.to_datetime(df[col], utc=True) # simplified: assume UTC
def prepare(data):
log = pm4py.format_dataframe(
data.copy(),
case_id="request_id",
activity_key="step",
timestamp_key="finished_at",
)
print("rows:", len(data), "->", len(log))
return log
log_end = prepare(df)
Check: the screen must show rows: 16 -> 15. With client data, find out which steps the dropped rows cluster in before you go further.
Losing a row in the middle of a case makes two steps that were never adjacent appear to be. If R1’s Check row vanishes, the graph gains a Receive → Approve edge that the real process does not contain.
Step 4: Do the first run and get the wrong answer
discover_performance_dfg returns the graph along with its start and end activities. By default, each pair of activities comes with a dict containing mean, median, max, min, sum and stdev, so you can do your own ranking. Add this to the end of the file:
def rank(log, **options):
perf, start, end = pm4py.discover_performance_dfg(log, **options)
ordered = sorted(perf.items(),
key=lambda item: item[1]["median"], reverse=True)
for (a, b), m in ordered:
hours = {k: round(m[k] / 3600, 1)
for k in ("median", "mean", "sum")}
print(a, "->", b, hours)
return perf, start, end
print("Run 1: finish times only")
rank(log_end)
Check: Receive → Check must rank first with a median of 5 hours, while Check → Approve shows 2.5 hours. Compared with the hand-calculated answer, this ranking is wrong. Those five hours combine 1 hour of waiting with 4 hours of actual checking.
The CSV does have a started_at column, but the function looks only for the exact name start_timestamp. A column with the wrong name might as well not exist.
Step 5: Rename one column and the conclusion changes
df2 = df.rename(columns={"started_at": "start_timestamp"})
log_full = prepare(df2)
print("Run 2: real start times")
rank(log_full)
The interval now runs from the end of one step to the start of the next. Check → Approve rises to first place with a median of 2 hours; Receive → Check falls to 1 hour.
By mean, however, Receive → Check comes out at 13.6 hours, because R5 alone contributes 64 of the 68 hours in total. PM4Py uses the mean by default when drawing a performance DFG. Send the client the default picture and you will be pointing to a different edge from the one at the top of your own median table.
Step 6: Take out the weekend, then draw
The business_hours option is off by default. When it is on, the built-in calendar runs Monday to Friday, 7:00–17:00, and can be replaced with business_hour_slots, measured in seconds from the start of the week.
print("Run 3: business hours")
perf, start, end = rank(log_full, business_hours=True)
pm4py.save_vis_performance_dfg(
perf, start, end, "waiting.svg",
aggregation_measure="median",
)
R5 now waits only 2 working hours: half an hour on Friday afternoon and 1.5 hours on Monday morning. Calculated by hand, the three runs come out as follows:
| Run | Receive → Check (median / mean) | Check → Approve (median / mean) |
|---|---|---|
| Finish times only | 5 / 17.6 hours | 2.5 / 2.5 hours |
| With start times | 1 / 13.6 hours | 2 / 2 hours |
| With business hours added | 1 / 1.2 hours | 2 / 2 hours |
By run 3, both median and mean point to Check → Approve. Check: open waiting.svg and compare the numbers on the edges with the table. If the business-hours result is off, look at time zones first; this script simplifies matters by treating every timestamp as UTC.
Three traps in real data
Zero hours of waiting is not necessarily good news. PM4Py’s code does not let the start of the later step fall before the finish of the earlier one, so overlapping steps also show up as 0. For edges at zero, open a few of the original cases to tell work done in parallel from timestamps recorded wrongly.
A high median is not necessarily a big problem either. Divide sum by mean to see how many times an edge occurs: in run 2, 68 divided by 13.6 is 5. If the mean is 0, count directly in the log. An edge with a three-day wait that occurs only twice a year rarely deserves to head the report.
Raise licensing early. The open-source version of PM4Py on GitHub uses AGPL-3.0, and there is a separate version for commercial use in closed-source settings. Before putting the library into a client’s product, pass this detail to the legal team.
Once you have the number, ask the client why
The script finds where cases wait but does not say why. The study “Why am I Waiting?” divides the causes of waiting between activities into five types: batching, resource contention, prioritization, resource unavailability and extraneous factors. The logs in that study also contain both start and end times.
For Check → Approve, turn the five types into questions. Does the approver collect cases and process them in one go? Are they busy with other cases, giving way to urgent work, or not yet on shift? Are they waiting for someone outside the process? Each answer leads to further data to request: the name of the person handling the case, shift rosters, priority levels.
When you receive a log, the first job is to sit down with the person who runs the process, pick five cases and ask when each timestamp was recorded. “Start” may mean the moment a screen was opened, or the moment the case dropped into a queue. If it is the latter, renaming the column as in step 5 will still give the wrong result.
When job hunting, watch for FDE postings that mention process mining, workflow or operational improvement. On your CV, include only what you can explain if asked, for example: “Separated waiting time from processing time on a simulated event log; identified three assumptions that change the bottleneck”.
The client may ask nothing about the algorithm. But before you present the number, you need to be able to say when its clock starts.
Was this article useful?
Thanks for the feedback!
10 sources
- process-intelligence-solutions/pm4py (GitHub README)
- pm4py · PyPI · 2026-11-24
- How to Install PM4Py - Windows, macOS and Linux
- PM4Py Examples: Handling Event Data
- pm4py/utils.py (nhánh release)
- PM4Py Examples: Process Discovery
- pm4py/vis.py (nhánh release)
- pm4py/algo/discovery/dfg/adapters/pandas/df_statistics.py (nhánh release)
- pm4py/util/constants.py (nhánh release)
- Why am I Waiting? Data-Driven Analysis of Waiting Times in Business Processes · 2022-12-02