Fill gaps in an unevenly spaced pandas time series with time-weighted interpolation
Quick Overview
Fill missing readings in an unevenly spaced pandas time series using time-weighted interpolation and return a new DataFrame. It tests how interpolation weights estimates by elapsed time, how that differs from row-based filling, and how missing values at the edges of the series are handled.
Fill gaps in an unevenly spaced pandas time series with time-weighted interpolation
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `interpolate_time(df)` for a DataFrame indexed by a sorted, unevenly spaced `DatetimeIndex`, with one `float64` column `'temp'` that contains `NaN` values. Fill the gaps by time-weighted interpolation, where each estimate is weighted by the timestamp distance to the neighboring readings, and return a new DataFrame.
Requirements:
- Fill the `NaN` values in `'temp'` with `interpolate(method='time')`.
- Keep the same `DatetimeIndex` and column, with `'temp'` as `float64`.
- Do not modify `df` in place; work on a copy.
- Leading `NaN` values (before the first valid reading) may remain `NaN`.
```python
idx = pd.to_datetime(["2026-01-01 00:00", "2026-01-01 01:00", "2026-01-01 05:00"])
df = pd.DataFrame({"temp": [10.0, np.nan, 50.0]}, index=idx)
interpolate_time(df)
# temp
# 2026-01-01 00:00:00 10.0
# 2026-01-01 01:00:00 18.0 # 1h of a 5h gap -> 10 + (40 * 1/5)
# 2026-01-01 05:00:00 50.0
```
```hint Check the weights
In the example, work out what fraction of the time between the two known readings has passed at 01:00, and compare that with the fraction of rows.
```
```hint Look at the edges
Try a series that ends in `NaN` and check what pandas puts after the last valid reading when you use the default arguments.
```
### What a Strong Answer Covers
- Interpolates on a copy and returns a frame with the same index, the same column and `'temp'` as `float64`.
- Derives the 01:00 value in the example from the elapsed-time weights.
- Explains how the result differs from row-position interpolation when timestamps are unevenly spaced.
- States what happens to leading and trailing missing values with the default arguments and how to control it.
- Names the preconditions on the index and what to check before calling the method.
### Follow-up Questions
- How does the function treat leading and trailing `NaN` values, and how would you choose a policy for them?
- Why prefer `method='time'` over `method='linear'` here?
- How would you stop the function from filling a gap that spans many hours, for example when the sensor was offline for a day?
Overview: Fill missing readings in an unevenly spaced pandas time series using time-weighted interpolation and return a new DataFrame. It tests how interpolation weights estimates by elapsed time, how that differs from row-based filling, and how missing values at the edges of the series are handled.
Implement interpolate_time(df) for a DataFrame indexed by a sorted, unevenly spaced DatetimeIndex, with one float64 column 'temp' that contains NaN values. Fill the gaps by time-weighted interpolation, where each estimate is weighted by the timestamp distance to the neighboring readings, and return a new DataFrame.
Requirements:
Fill the
NaN
values in
'temp'
with
interpolate(method='time')
.
Keep the same
DatetimeIndex
and column, with
'temp'
as
float64
.
Do not modify
df
in place; work on a copy.
Leading
NaN
values (before the first valid reading) may remain
NaN
.