Estimate interior numeric gaps
A numeric column contains occasional missing measurements between valid readings.
Pandas · Module
Fill missing DataFrame values by interpolation along an axis. The method supports controls for interpolation strategy, gap size, direction, region, and in-place updates.
DataFrame.interpolate fills missing values by estimating them from neighboring observations along the selected axis. Choose a method such as linear or time, and optionally constrain how many consecutive missing values are filled, which direction is used, or whether only interior or exterior gaps qualify.
DataFrame.interpolate(*method='linear', axis=0, limit=None, inplace=False, limit_direction=None, limit_area=None, **kwargs)| Name | Type | Description |
|---|---|---|
method | str, default 'linear' | Interpolation strategy, such as linear, time, index, values, polynomial, or spline. |
axis | 0 or 'index', 1 or 'columns', None, default 0 | Axis along which to interpolate: 0/'index' or 1/'columns'. |
limit | int, optional | Maximum number of consecutive NaNs to fill; must be greater than zero. |
limit_direction | 'forward', 'backward', or 'both', optional | Direction for filling consecutive NaNs: forward, backward, or both. |
limit_area | 'inside' or 'outside', optional | Restrict filling to interior or exterior NaN regions. |
inplace | bool, default False | Modify the DataFrame in place when possible. |
**kwargs | dict | Additional keyword arguments passed to the interpolation implementation; for example, order for polynomial or spline. |
DataFrame or None. A new interpolated DataFrame is returned unless inplace=True, in which case the method returns None.
import pandas as pd
values = pd.DataFrame({"temperature": [10.0, None, None, 16.0]})
filled = values.interpolate(limit=1)
print(filled)Interpolates one missing value in each consecutive gap, leaving any additional values in a longer gap unchanged.
A numeric column contains occasional missing measurements between valid readings.
Observations are indexed by dates and missing periods should reflect elapsed time.
Only short runs of missing values should be repaired while larger gaps remain visible.
A pipeline needs to update an existing DataFrame without assigning the result.
It fills NaN values along the chosen axis using an interpolation method. The default method is linear, and the default axis is 0.
Follow-up: How would you restrict filling to a maximum gap length? Pass a positive limit; larger consecutive gaps are only partially filled according to limit_direction.
method='linear' treats values as equally spaced and ignores the index. method='index' uses the numerical index values when calculating the line.
Follow-up: Which method is suitable for a time-indexed DataFrame? Use method='time' for daily or higher-resolution data when the interval between observations should determine the interpolation.
limit_direction controls whether consecutive missing values are filled forward, backward, or in both directions. It defaults to forward.
Follow-up: How can endpoint behavior be constrained? Use limit_area='inside' to fill only NaNs surrounded by valid values, or 'outside' to fill extrapolated endpoint gaps.
pandas.DataFrame.fillnapandas.Series.interpolatepandas.DataFrame.ffillpandas.DataFrame.bfill