Carry recent measurements forward
A time-ordered measurement table has short missing runs between observations.
Pandas · Module
DataFrame.ffill fills missing values by carrying the preceding valid observation forward along an axis. Optional limits control how many values or which regions are filled.
DataFrame.ffill scans along the selected axis and carries the most recent non-missing value into later missing positions. Use limit to bound filling, and limit_area to restrict it to interior gaps or values outside the observed range; with inplace=True, the method returns None and mutates the frame.
DataFrame.ffill(*, axis=None, inplace=False, limit=None, limit_area=None, downcast=<no_default>)| Name | Type | Description |
|---|---|---|
axis | int or {"index", "columns"}, default None | Axis to fill: 0 or "index" for Series; 0/"index" or 1/"columns" for DataFrame. |
inplace | bool, default False | Whether to modify the object directly. |
limit | int, default None | Maximum number of consecutive missing values to fill; must be greater than zero when provided. |
limit_area | {None, "inside", "outside"}, default None | Restriction for filling when limit is set: None, "inside", or "outside". |
downcast | dict or "infer", default <no_default> | Mapping of items to dtypes for possible downcasting, or "infer"; the documentation marks this parameter as deprecated. |
Series/DataFrame or None: the object with missing values filled, or None when inplace=True.
axis chooses whether propagation proceeds down the index or across DataFrame columns.inplace=True to mutate the existing object instead of receiving a new filled object.limit caps how many adjacent missing values are filled in each run.limit_area changes which missing regions are eligible when a limit is present.None for in-place operation.inplace=True, other views sharing the object can also observe the mutation.limit must be a positive integer.limit_area="outside" fills edge gaps rather than gaps enclosed by valid values.import pandas as pd
frame = pd.DataFrame({"temperature": [20.0, None, None, 23.0, None]})
filled = frame.ffill(limit=1)
print(filled)Fills each missing run from its preceding value, but replaces at most one consecutive missing entry per run.
A time-ordered measurement table has short missing runs between observations.
A bounded carry-forward is required so long gaps remain visibly missing.
Only holes enclosed by observations should be completed.
A row-wise record contains values spread across ordered columns.
DataFrame.ffill do, and what does it return?It propagates the last valid observation forward across missing values along the chosen axis. By default it returns a filled DataFrame; with inplace=True, it returns None.
Follow-up: How can filling be bounded? Set limit to the maximum number of consecutive missing entries to fill in a run.
axis affect forward filling?For a DataFrame, axis=0 or "index" fills down rows within each column, while axis=1 or "columns" fills across columns within each row. The default axis is index-oriented.
Follow-up: What happens for a Series? Its axis parameter is unused and defaults to 0.
limit_area?limit_area="inside" fills only missing values surrounded by valid values, while "outside" targets missing values outside valid values. This restriction applies when limit is specified.
Follow-up: How would you fill only interior gaps while limiting each gap to two entries? Use ffill(limit=2, limit_area="inside").
pandas.DataFrame.bfillpandas.DataFrame.fillnapandas.Series.ffill