Devices With the Longest Run of Daily Check-ins
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.
| ping_id | device_id | ping_day | energy_kwh |
|---|---|---|---|
| 1 | 101 | 2024-03-01 | 5 |
| 2 | 101 | 2024-03-02 | 6 |
| 3 | 101 | 2024-03-03 | 4 |
| 4 | 101 | 2024-03-10 | 9 |
| 5 | 202 | 2024-03-05 | 2 |
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_pingsSolve 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