AnalystPath

Devices With the Longest Run of Daily Check-ins

PandasHardSenior level~10 min

Problem

You are given one DataFrame, `thermostat_pings`, loaded from `thermostat_pings.csv` with columns `ping_id` (the primary key), `device_id`, `ping_day` (a calendar date), and `energy_kwh`. Each row records that a smart thermostat checked in once on a given day; a device checks in at most once per day.

A "run" is a maximal stretch of consecutive calendar days on which a device checked in (no gaps). Find the device(s) whose longest run is the longest among all devices. Return the `device_id` of each such device, ordered by `device_id`.

Input data

Example rows — the live problem includes the full dataset.

thermostat_pings
ping_iddevice_idping_dayenergy_kwh
11012024-03-015
21012024-03-026
31012024-03-034
41012024-03-109
52022024-03-052

Expected output

Your answer should return 2 rows with the columns device_id.

Starter code (Pandas (Python))

import pandas as pd

def longest_checkin_runs(thermostat_pings) -> pd.DataFrame:
    # Your code here
    return thermostat_pings

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