BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / pandas14 of 18 in topic

652. Resample a request log to hourly traffic, filling empty hours with zeros

Hardpandas Pro

Capacity planning for an LLM endpoint needs traffic per hour, and quiet hours matter as much as busy ones. Grouping by hour skips hours with no requests, which breaks charts and averages.

Write hourly_traffic(df) where df has columns ts ('YYYY-MM-DD HH:MM' string) and tokens (int), rows in arbitrary order.

Return a DataFrame with columns exactly [hour, requests, tokens, tokens_per_request], one row for EVERY hour from the hour of the earliest request to the hour of the latest, inclusive, sorted by hour, plain 0..n-1 index.
- hour: the hour start as a 'YYYY-MM-DD HH:00' string.
- requests: number of rows in that hour; tokens: their token sum (both 0 for empty hours).
- tokens_per_request: tokens / requests as a float, 0.0 when requests is 0.

Functiondef hourly_traffic(df)

Example 1

Input: requests at 10:05 (100 tokens), 10:40 (50) and 12:10 (30)

Output: hours 10:00, 11:00, 12:00 with requests 2, 0, 1, tokens 150, 0, 30 and tokens_per_request 75.0, 0.0, 30.0

Constraints

  • Python 3 with pandas. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

A plain groupby by hour skips hours with no requests, but resampling a time index creates every hour bin between the first and last request.

Hint 2

Parse ts with pd.to_datetime, set it as a sorted index, resample the tokens to 60 minute bins to get the count and the sum, compute tokens per request, and format the hour labels.

Hint 3

Divide by requests with zeros turned into NaN (for example .where(requests > 0)), then fillna(0.0); cast requests and tokens to int64 and build hour with index.strftime('%Y-%m-%d %H:00').

Understand the idea

For capacity planning you need traffic for every hour, including quiet ones. Grouping by hour only creates rows for hours that had requests, so empty hours vanish and charts and averages go wrong. Resampling builds a regular hourly grid from the first hour to the last and fills empty hours with zero counts and zero sums.

Worked example

requests at 09:15 (40 tokens), 09:50 (20) and 11:30 (60). Hours: 09:00, 10:00, 11:00. requests = [2, 0, 1], tokens = [60, 0, 60]. tokens_per_request = [30.0, 0.0, 60.0], where the empty hour gives 0.0 instead of a division by zero.

parse ts as datetimes and use them as the index; resample to 1 hour, counting rows and summing tokens; tokens_per_request = tokens / requests where requests > 0, else 0.0; format hour as 'YYYY-MM-DD HH:00'; output columns exactly [hour, requests, tokens, tokens_per_request], sorted, plain 0 to n-1 index

Input rows may be in any order, so sort first. Used when sizing GPU capacity for an LLM endpoint.

Topics
pandas
Similar questions
  • Compute a 3-day calendar rolling error rate per model with gaps in the dataHard
  • Flag metric spikes with a rolling z-score of the previous values per modelHard
  • Build a monthly cohort retention matrix with pandasHard
  • Price each API call with the rate in effect at that time using merge_asofHard
All pandas problems
Report
652 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import pandas as pd
​
def hourly_traffic(df):
# your code here
pass
​
hourly_traffic(_d1()).to_dict('list')

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS