AnalystPath

Machines With Quick Back-to-Back Runs

PandasHardSenior level~10 min

Problem

A DataFrame `runs` logs production runs on factory machines with columns `machine_id`, `run_start` and `run_end` (datetime strings), a unique `run_id`, and a `job_type` (such as `Mold` or `Cut`). A run is "quick back-to-back" when the same machine starts another run of the same `job_type` no more than 8 hours after the previous run of that same type ended.

Return a DataFrame with the distinct `machine_id` of every machine that has at least one quick back-to-back run, ordered ascending.

Input data

Example rows — the live problem includes the full dataset.

runs
machine_idrun_startrun_endrun_idjob_type
1012024-03-01 08:00:002024-03-01 09:00:001Mold
1012024-03-01 10:00:002024-03-01 11:00:002Cut
1022024-03-01 13:00:002024-03-01 14:00:003Mold
1022024-03-01 15:00:002024-03-01 16:00:004Mold
1012024-03-02 09:00:002024-03-02 10:00:005Mold

Expected output

Your answer should return 1 row with the columns machine_id.

Starter code (Pandas (Python))

import pandas as pd

def quick_machines(runs) -> pd.DataFrame:
    # Your code here
    return runs

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions