Q&A library Python

How do I fill gaps in a series by interpolating in pandas?

Python Advanced interpolatemissing-datatime-series

Short answer

Use interpolate to estimate missing values from the ones around them, linearly by default. It suits ordered numeric data like time series, and you should sort first and consider limiting how far it fills.

The short version

interpolate fills missing values by estimating them from the surrounding known values, drawing a straight line between neighbours by default. It suits ordered numeric data, above all time series. Sort the data into order first, because interpolation assumes the rows are in sequence, and consider capping how far it will fill so it does not invent long stretches of data.

A worked example

import pandas as pd
import numpy as np

readings = pd.Series([1.0, np.nan, np.nan, 4.0])

print(readings.interpolate())
0    1.0
1    2.0
2    3.0
3    4.0
dtype: float64

The two gaps between 1 and 4 are filled with the evenly spaced 2 and 3, because linear interpolation draws a straight line between the known ends.

How it differs from fillna

fillna inserts a fixed value or carries a neighbour forward, so a gap becomes a flat repeat. interpolate instead follows the trend between the known points, which is usually more sensible for a measured quantity that changes smoothly, like temperature. Use fillna when the missing value should be a constant or the last known reading; use interpolate when it should follow the surrounding shape.

The ordering assumption

Interpolation reads neighbours by position, so out-of-order rows produce nonsense. Sort by the time or sequence column first.

df = df.sort_values("date")
df["value"] = df["value"].interpolate()

Do not fill the ends by default

By default, interpolate cannot fill a leading gap, because there is no earlier value to interpolate from, so a NaN at the very start remains. That is deliberate. Be wary of forcing it to fill the edges, because extrapolating beyond your data is a much stronger claim than filling between known points.

Limiting how far it fills

A single missing reading is reasonable to interpolate. A gap of fifty rows is probably not, because the estimate becomes fiction. Cap the run with limit.

readings.interpolate(limit=2)   # fill at most two consecutive gaps

Other methods

The default is linear. For data where a curve fits better, method="time" spaces values by actual date gaps rather than row count, and method="polynomial" or "spline" fit curves. Reach for these only when you can justify the shape, because a fancier interpolation makes a stronger assumption about how the missing values behave.

Need this solved on your own data?

The library is free and always will be. When the problem is bigger than an answer, we take on the work.

Start a project