pandas.Series.ffill#

Series.ffill(*, axis=None, inplace=False, limit=None, limit_area=None)[source]#

Fill NA/NaN values by propagating the last valid observation to next valid.

This method fills missing values using forward fill, where the last valid observation is propagated forward to fill the gaps.

Parameters:
axis{0 or ‘index’} for Series, {0 or ‘index’, 1 or ‘columns’} for DataFrame

Axis along which to fill missing values. For Series this parameter is unused and defaults to 0.

inplacebool, default False

If True, fill in-place. Note: this will modify any other views on this object (e.g., a no-copy slice for a column in a DataFrame).

limitint, default None

Maximum number of consecutive NaN values to fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. Must be greater than 0 if not None.

limit_area{None, ‘inside’, ‘outside’}, default None

Restrict which NaNs are filled based on their position relative to the valid values.

  • None: No fill restriction.

  • ‘inside’: Only fill NaNs surrounded by valid values (interpolate).

  • ‘outside’: Only fill NaNs outside valid values (extrapolate).

Added in version 2.2.0.

Returns:
Series/DataFrame

Object with missing values filled.

See also

DataFrame.bfill

Fill NA/NaN values by using the next valid observation to fill the gap.

Examples

>>> df = pd.DataFrame(
...     [
...         [np.nan, 2, np.nan, 0],
...         [3, 4, np.nan, 1],
...         [np.nan, np.nan, np.nan, np.nan],
...         [np.nan, 3, np.nan, 4],
...     ],
...     columns=list("ABCD"),
... )
>>> df
     A    B   C    D
0  NaN  2.0 NaN  0.0
1  3.0  4.0 NaN  1.0
2  NaN  NaN NaN  NaN
3  NaN  3.0 NaN  4.0
>>> df.ffill()
     A    B   C    D
0  NaN  2.0 NaN  0.0
1  3.0  4.0 NaN  1.0
2  3.0  4.0 NaN  1.0
3  3.0  3.0 NaN  4.0
>>> ser = pd.Series([1, np.nan, 2, 3])
>>> ser.ffill()
0   1.0
1   1.0
2   2.0
3   3.0
dtype: float64