← All Python interview questions
Resample daily orders into a monthly summary
MediumDates & Time SeriesMyntraBigBasket
Ops wants monthly totals from a daily order log, including months where nothing happened.
Write monthly_totals(df) that returns a Series indexed by month-end dates with the summed amount per month, and 0.0 for months with no orders.
This fixture runs before your code on every Run and Submit, so the data is always clean.
import pandas as pd
daily = pd.DataFrame({
"order_date": pd.to_datetime([
"2026-01-05", "2026-01-20", "2026-03-02", "2026-03-15", "2026-03-28", "2026-04-09",
]),
"amount": [1000.0, 500.0, 250.0, 750.0, 1000.0, 2000.0],
})- Step 1: `resample` needs a DatetimeIndex, so `set_index('order_date')` comes first and `sort_index()` guards against unordered input.
- Step 2: `resample('ME')` buckets by month end and, unlike `groupby(month)`, it inserts the empty February bucket for you.
- Step 3: `.sum()` on an empty bucket already yields `0.0`; the `fillna(0.0)` is belt-and-braces for other aggregations.
def monthly_totals(df):
s = df.set_index("order_date")["amount"].sort_index()
return s.resample("ME").sum().fillna(0.0)Learn the concept first
Stuck on Dates & Time Series? These free Python lessons teach it step by step.
More Dates & Time Series questions
Asked at Myntra
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program