Upsample a sparse pandas time series to daily frequency with linear interpolation

Quick Overview

Upsample a sparse pandas Series with a DatetimeIndex to a complete daily index and fill the new days by linear interpolation. It tests resample and interpolate, how observed values are kept, and how edge gaps and off-grid timestamps behave.

Upsample a sparse pandas time series to daily frequency with linear interpolation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `upsample_interpolate(s: pd.Series) -> pd.Series`. Given a numeric Series on a sparse `DatetimeIndex`, upsample it to daily frequency (`s.resample('D')`) and fill the new dates with `.interpolate()` (default `'linear'`). The result must meet these requirements: - The index is a complete, contiguous daily `DatetimeIndex` from the original minimum date to the maximum date. - Original observed values are preserved at their dates. - Intermediate days are linearly interpolated between adjacent known points. For example: ```python s = pd.Series( [10.0, 40.0], index=pd.to_datetime(['2024-01-01', '2024-01-04']) ) upsample_interpolate(s) # 2024-01-01 10.0 # 2024-01-02 20.0 # 2024-01-03 30.0 # 2024-01-04 40.0 # Freq: D, dtype: float64 ``` ```hint Look before you fill Write down what the upsampled Series holds before any filling: which dates exist, and which of them already carry a value. ``` ### Constraints & Assumptions - Timestamps are whole dates (midnight), unique, and sorted. - Gaps between observations can be any number of days. ### What a Strong Answer Covers - Building a daily index from the first to the last observed date with no missing or extra days. - Keeping each observed value unchanged at its date. - Filling each gap along the straight line between the two neighboring observations, and why plain linear interpolation is correct on an evenly spaced grid. - What happens to edge NaNs and to timestamps that do not sit on the daily grid. ### Follow-up Questions - How would leading or trailing NaNs behave, and how could you handle them? - When would you use time-aware interpolation instead of plain linear? - How would you handle observations that carry a time of day, such as `2024-01-02 15:00`?

Overview: Upsample a sparse pandas Series with a DatetimeIndex to a complete daily index and fill the new days by linear interpolation. It tests resample and interpolate, how observed values are kept, and how edge gaps and off-grid timestamps behave.

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

Implement upsample_interpolate(s: pd.Series) -> pd.Series. Given a numeric Series on a sparse DatetimeIndex, upsample it to daily frequency (s.resample('D')) and fill the new dates with .interpolate() (default 'linear').

The result must meet these requirements:

  • The index is a complete, contiguous daily DatetimeIndex from the original minimum date to the maximum date.
  • Original observed values are preserved at their dates.
  • Intermediate days are linearly interpolated between adjacent known points.

For example:

s = pd.Series(
    [10.0, 40.0],
    index=pd.to_datetime(['2024-01-01', '2024-01-04'])
)

upsample_interpolate(s)
# 2024-01-01    10.0
# 2024-01-02    20.0
# 2024-01-03    30.0
# 2024-01-04    40.0
# Freq: D, dtype: float64

Constraints & Assumptions

  • Timestamps are whole dates (midnight), unique, and sorted.
  • Gaps between observations can be any number of days.

What a Strong Answer Covers Guidance

  • Building a daily index from the first to the last observed date with no missing or extra days.
  • Keeping each observed value unchanged at its date.
  • Filling each gap along the straight line between the two neighboring observations, and why plain linear interpolation is correct on an evenly spaced grid.
  • What happens to edge NaNs and to timestamps that do not sit on the daily grid.

Follow-up Questions Guidance

  • How would leading or trailing NaNs behave, and how could you handle them?
  • When would you use time-aware interpolation instead of plain linear?
  • How would you handle observations that carry a time of day, such as 2024-01-02 15:00 ?