Knowledge base / Python
pandas interpolate: linear, time and filling limits
I show how to estimate missing numbers while distinguishing estimates from measurements and unrestricted boundary filling.

Interpolation in pandas estimates missing numbers from available points. I begin by asking whether that estimate makes sense for the variable and the reason it is missing. A smooth connection between measurements does not automatically reconstruct what happened. A filled value remains an estimate even when it looks identical to a measured value in a table.
This entry uses DataFrame.interpolate() and Series.interpolate() according to the pandas 3.1.1 documentation. All examples are synthetic with float64 data. I focus on simple linear arithmetic, the difference between position and elapsed time, and restrictions on filling. There is no standalone pd.interpolate() function in this interface.
Linear interpolation down columns
The default linear method treats rows as equally spaced and ignores index values. For a DataFrame, axis=0 works down each column. The interpolate documentation describes the methods and axis choice:
import pandas as pd
df = pd.DataFrame({
"A": [1, None, 3, None, 5],
"B": [2, 4, None, 8, 10],
}, dtype="float64")
filled = df.interpolate(method="linear", axis=0)
| Index | A before | B before | A after | B after |
|---|---|---|---|---|
| 0 | 1 | 2 | 1.0 | 2.0 |
| 1 | NaN | 4 | 2.0 | 4.0 |
| 2 | 3 | NaN | 3.0 | 6.0 |
| 3 | NaN | 8 | 4.0 | 8.0 |
| 4 | 5 | 10 | 5.0 | 10.0 |
The first gap in A is halfway between 1 and 3, giving 2. The second is between 3 and 5, giving 4. In B, the gap between 4 and 8 becomes 6. Available values remain unchanged. The result is stored as filled, while the original df remains available because I have not enabled inplace=True.
I would not switch to axis=1 without considering the columns. That interpolates across each row. If columns represent temperature, order count and an identifier, there is no meaningful shared scale for that calculation. First select numeric columns representing comparable measurements. The method cannot infer which fields are quantities and which are merely codes.
Row position and elapsed time
With irregular dates, the middle row need not lie halfway in time between measurements. Series.interpolate distinguishes linear, time and index. Suppose readings of 0 and 40 are four days apart, with a missing observation one day after the first:
t = pd.Series(
[0.0, None, 40.0],
index=pd.to_datetime(["2026-01-01", "2026-01-02", "2026-01-05"]),
)
by_position = t.interpolate(method="linear")
by_time = t.interpolate(method="time")
For January 2, linear gives 20 because the missing row lies halfway between two row positions. The time method gives 10: one of four days has elapsed, so the increase is one quarter of 40. These are different definitions of distance. Choose the one matching the variable’s model. With a numeric index, method="index" uses index values rather than row positions.
Interpolation fills existing gaps but does not insert absent dates into the index. If there is no row for January 3, neither result creates it automatically. Preparing a regular time grid requires a separate frequency operation, such as the one discussed in resample.

Limit gaps and boundary filling
The limit parameter restricts consecutive filled gaps, while limit_direction chooses the direction. For 0, NaN, NaN, NaN, 40, linear interpolation with limit=1 and limit_direction="forward" gives 0, 10, NaN, NaN, 40. With direction both, it gives 0, 10, NaN, 30, 40. A long gap can therefore be partially filled. A limit of one does not mean “fill only complete gaps of length one.”
To restrict filling to gaps surrounded by available values, use limit_area="inside". Here is an example with single gaps:
edge = pd.Series([None, 10, None, 30, None], dtype="float64")
inside = edge.interpolate(
method="linear", limit=1,
limit_direction="forward", limit_area="inside",
)
The result is NaN, 10, 20, 30, NaN. Both ends stay missing. Without the area restriction, default forward linear interpolation can fill the trailing gap with the last available value. I would not call that an estimated continuation of a trend. Boundary behaviour depends on the method, so I do not transfer that description automatically to every SciPy variant.
Why the old spline example is removed
Methods such as polynomial, nearest and spline use SciPy. Polynomial and spline methods also require an order. The UnivariateSpline documentation requires more available points than the spline degree. The old example attempted degree 3 with only three known points in one column. It does not meet that requirement and is unsuitable as a copyable example.
I keep this entry’s examples linear, with visible arithmetic. A higher degree does not automatically produce a more credible estimate and may need different assumptions about the process. For nonlinear interpolation, choose the method from the process, available point count and SciPy requirements. One replacement command is not a universal repair for all columns. The current pandas signature also omits the old downcast parameter; the pandas 3.0 release notes document API changes.
Preserve a record of estimated values
Before filling, I save a missing-value mask such as missing_before = df.isna() and retain the original data. Subsequent analysis can then distinguish measured values from estimates. The missing-data guide explains missing-value detection. A text label such as “missing” needs separate type preparation before it behaves as numeric missingness.
Interpolation using a later point is not automatically appropriate for features that must be available earlier. For modelling, check whether filling uses future information or data held out for evaluation. I also do not interpolate categories, identifiers or every zero. Zero can be a valid measurement, and the reason for a gap may undermine the assumption of a smooth transition.
For an exercise, change the middle date from January 2 to January 4. With the same endpoints of 0 and 40, time should give 30 while linear still gives 20. Explain the difference using the fraction of elapsed time. Then decide how the report will mark estimated values and which gaps will remain unfilled. These decisions matter more than simply eliminating every NaN.
Key takeaways
Does linear account for elapsed time?
It treats rows as equally spaced. With a suitable datetime index, time interpolation uses actual elapsed time.
Does limit=1 reject every longer gap?
A longer gap can be partially filled according to limit_direction. The parameter limits consecutive fills rather than testing an entire gap length.
Why does spline need more data?
The available point count must exceed the spline degree. It needs SciPy and an appropriate order; three points are insufficient for degree 3.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
