Upsample a sparse pandas Series to a daily index with forward fill

Quick Overview

Upsample a sparse pandas Series, such as weekly data, to a complete daily index where each new day carries the most recent prior value. It tests resample with forward fill, how it differs from asfreq, and how limits and missing observations change the result.

Upsample a sparse pandas Series to a daily index with forward fill

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `upsample_ffill(s: pd.Series) -> pd.Series`. The input is a Series with a sparse `DatetimeIndex`, for example weekly. Return it upsampled to a complete daily index from the first to the last original date, with each new day carrying the most recent prior value. The function must: - Resample with `s.resample('D')`. - Forward-fill the new days with `.ffill()`. For example: ```python s = pd.Series( [10, 20, 30], index=pd.to_datetime(["2024-01-01", "2024-01-08", "2024-01-15"]), ) upsample_ffill(s) # 2024-01-01 10 # 2024-01-02 10 # ... # 2024-01-08 20 # ... # 2024-01-15 30 # Freq: D, dtype: int64 ``` ```hint Read the dtype The example output is still `int64`. Work out what that tells you about whether the new days ever hold `NaN` on the way to the result. ``` ### Constraints & Assumptions - The index is a sorted `DatetimeIndex` with unique timestamps at whole dates. - Gaps between observations can be any number of days. ### What a Strong Answer Covers - Building a daily index that runs from the first to the last original date. - Giving each new day the value of the latest observation on or before it, while observed dates keep their own values. - Why the example result keeps the integer dtype. - How missing observations and very long gaps are treated. ### Follow-up Questions - How does this differ from `.asfreq('D')`? - What if you want to cap how far a value is carried forward? - One observed value is itself `NaN`. What does the result show on that date and the days after it, and how would you fill it from the previous value instead?

Overview: Upsample a sparse pandas Series, such as weekly data, to a complete daily index where each new day carries the most recent prior value. It tests resample with forward fill, how it differs from asfreq, and how limits and missing observations change the result.

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

Implement upsample_ffill(s: pd.Series) -> pd.Series. The input is a Series with a sparse DatetimeIndex, for example weekly. Return it upsampled to a complete daily index from the first to the last original date, with each new day carrying the most recent prior value.

The function must:

  • Resample with s.resample('D') .
  • Forward-fill the new days with .ffill() .

For example:

s = pd.Series(
    [10, 20, 30],
    index=pd.to_datetime(["2024-01-01", "2024-01-08", "2024-01-15"]),
)

upsample_ffill(s)
# 2024-01-01    10
# 2024-01-02    10
# ...
# 2024-01-08    20
# ...
# 2024-01-15    30
# Freq: D, dtype: int64

Constraints & Assumptions

  • The index is a sorted DatetimeIndex with unique timestamps at whole dates.
  • Gaps between observations can be any number of days.

What a Strong Answer Covers Guidance

  • Building a daily index that runs from the first to the last original date.
  • Giving each new day the value of the latest observation on or before it, while observed dates keep their own values.
  • Why the example result keeps the integer dtype.
  • How missing observations and very long gaps are treated.

Follow-up Questions Guidance

  • How does this differ from .asfreq('D') ?
  • What if you want to cap how far a value is carried forward?
  • One observed value is itself NaN . What does the result show on that date and the days after it, and how would you fill it from the previous value instead?