AnalystPath

Longest Steady Thermostat Streak

PandasHardSenior level~10 min

Problem

You are given one DataFrame `thermostat_log` (loaded from `thermostat_log.csv`), recording every thermostat set with columns `home_id`, `set_date`, and `mode` ('heat', 'cool', 'eco', or 'off'). A household is steady over a run of days when, for at least 5 consecutive calendar days, it set the thermostat exactly once per day AND chose the same mode on every one of those days. For each household with at least one such run, return its single longest steady run: the `home_id`, the `mode` held, the run length in days as `run_length`, and the run's `start_date` and `end_date`. If a household has two runs of equal length, keep the earlier-starting one. Order by `run_length` descending, then `home_id` ascending.

Input data

Example rows — the live problem includes the full dataset.

thermostat_log
home_idset_datemode
12024-01-01heat
12024-01-02heat
12024-01-03heat
12024-01-04heat
12024-01-05heat

Expected output

Your answer should return 2 rows with the columns home_id, mode, run_length, start_date, end_date.

Starter code (Pandas (Python))

import pandas as pd

def find_longest_steady_streak(thermostat_log) -> pd.DataFrame:
    # Your code here
    return thermostat_log

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