Sub topic 7
Filling gaps with ffill, bfill, and interpolate
This chapter assumes you can build a DatetimeIndex and know what upsampling is: moving to a finer frequency, such as weekly to daily. Upsampling creates rows that have no source data. This chapter covers how to fill them.
Why upsampling leaves gaps
When you resample weekly data to daily, pandas builds a daily grid. Only the original timestamps have values. Every other row is NaN. Calling resample("D") alone returns a Resampler object, so you must follow it with a method that decides what goes in the new rows.
The three fill methods
import pandas as pd
idx = pd.date_range("2024-01-01", periods=3, freq="W-MON")
s = pd.Series([10.0, 20.0, 30.0], index=idx)
print(s.resample("D").ffill())
print(s.resample("D").interpolate())ffill()(forward fill) copies the last known value forward. For 2024-01-02 through 2024-01-07 it gives10.0. Use it for values that hold until changed, such as a price list or a configuration setting.bfill()(backward fill) copies the next known value backward. The same days get20.0. Use it when a value applies to the period before its timestamp.interpolate()estimates new values between known points. The default is linear. Each day rises by 10/7, so the first rows are 10.000000, 11.428571, 12.857143, 14.285714, and so on, reaching 20.0 on 2024-01-08.
Use ffill for stepwise data, such as a status or a rate. Use interpolate for smooth quantities, such as temperature. Do not interpolate counts or categories.
Leaving gaps with asfreq
s.resample("D").asfreq() creates the daily grid and leaves NaN in every new row. This is useful when you want to see where data is missing, or fill it yourself later.
Limiting how far a fill reaches
Carrying a value forward for weeks can hide a sensor outage. Pass limit to cap the number of consecutive rows filled.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=3, freq="W-MON")
s = pd.Series([10.0, 20.0, 30.0], index=idx)
print(s.resample("D").ffill(limit=2))Only 2024-01-02 and 2024-01-03 get 10.0. The remaining days up to 2024-01-07 stay NaN.
Edges of the series
ffill leaves leading rows as NaN, because nothing comes before the first value. bfill leaves trailing rows as NaN for the same reason. Linear interpolate does not extrapolate past the last known value by default. Here the series ends exactly on a known timestamp, so no trailing gap appears.
Quick comparison
| Method | New row gets | Typical use |
|---|---|---|
| `asfreq()` | `NaN` | Inspect gaps |
| `ffill()` | Previous value | Stepwise data |
| `bfill()` | Next value | Period-ahead values |
| `interpolate()` | Linear estimate | Smooth measurements |
Exercise
Using the weekly series [10.0, 20.0, 30.0] starting 2024-01-01 and resampled to "D", what value does 2024-01-04 get under ffill(), bfill(), and interpolate()?
Show answer
ffill() gives 10.0 (the last known value) and bfill() gives 20.0 (the next known value). interpolate() gives 10 + 3 * (10 / 7), about 14.285714, because 2024-01-04 is three days into a seven-day rise from 10 to 20.