Weekly Mean With Pandas resample on a Timestamp Column (on= Keyword)

Quick Overview

Write a pandas function that returns the weekly mean of a quantity column when the timestamps sit in a regular column instead of the index. It tests the resample on= keyword, how weekly bins are anchored, closed and labelled, and how empty weeks show up.

Weekly Mean With Pandas resample on a Timestamp Column (on= Keyword)

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `resample_on_column(df: pd.DataFrame) -> pd.Series` returning the weekly mean of `qty`, where timestamps live in the regular column `ts` (not the index). The function must meet these requirements: - Use `df.resample('W', on='ts')['qty'].mean()` without setting `ts` as the index. - Return a `pd.Series` indexed by a weekly `DatetimeIndex`, with bins labelled by the week-ending Sunday (`freq='W-SUN'`). Example: ```python df = pd.DataFrame({ 'ts': pd.to_datetime(['2024-01-01', '2024-01-03', '2024-01-08']), 'qty': [10, 20, 30], }) resample_on_column(df) # ts # 2024-01-07 15.0 # mean of Jan 1 & Jan 3 # 2024-01-14 30.0 # Freq: W-SUN, Name: qty, dtype: float64 ``` ```hint Bin edges 2024-01-01 is a Monday. Work out which side of each weekly bin is closed by default for `'W'` and which edge becomes the label. ``` ### What a Strong Answer Covers - The `resample` call with `on='ts'` and the mean over `qty`. - How the default `'W'` rule anchors on Sunday, which side of each bin is closed, and which edge is used as the label. - The index, name and dtype of the returned Series. - What happens for weeks without rows and for a `ts` column that is not datetime typed. ### Follow-up Questions - How would you switch the bins to end on Monday instead of Sunday? - What if some weeks have no rows? - How would you compute the same weekly mean separately for each value of a `store` column?

Overview: Write a pandas function that returns the weekly mean of a quantity column when the timestamps sit in a regular column instead of the index. It tests the resample on= keyword, how weekly bins are anchored, closed and labelled, and how empty weeks show up.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write resample_on_column(df: pd.DataFrame) -> pd.Series returning the weekly mean of qty, where timestamps live in the regular column ts (not the index).

The function must meet these requirements:

  • Use df.resample('W', on='ts')['qty'].mean() without setting ts as the index.
  • Return a pd.Series indexed by a weekly DatetimeIndex , with bins labelled by the week-ending Sunday ( freq='W-SUN' ).

Example:

df = pd.DataFrame({
    'ts': pd.to_datetime(['2024-01-01', '2024-01-03', '2024-01-08']),
    'qty': [10, 20, 30],
})
resample_on_column(df)
# ts
# 2024-01-07    15.0   # mean of Jan 1 & Jan 3
# 2024-01-14    30.0
# Freq: W-SUN, Name: qty, dtype: float64

What a Strong Answer Covers Guidance

  • The resample call with on='ts' and the mean over qty .
  • How the default 'W' rule anchors on Sunday, which side of each bin is closed, and which edge is used as the label.
  • The index, name and dtype of the returned Series.
  • What happens for weeks without rows and for a ts column that is not datetime typed.

Follow-up Questions Guidance

  • How would you switch the bins to end on Monday instead of Sunday?
  • What if some weeks have no rows?
  • How would you compute the same weekly mean separately for each value of a store column?