pandas.api.typing.Resampler.ffill#
- final Resampler.ffill(limit=None)[source]#
Forward fill the values.
This method fills missing values by propagating the last valid observation forward, up to the next valid observation. It is commonly used in time series analysis when resampling data to a higher frequency (upsampling) and filling gaps in the resampled output.
- Parameters:
- limitint, optional
Limit of how many values to fill.
- Returns:
- Series
The resampled data with missing values filled forward.
See also
Series.fillnaFill NA/NaN values using the specified method.
DataFrame.fillnaFill NA/NaN values using the specified method.
Examples
Here we only create a
Series.>>> ser = pd.Series( ... [1, 2, 3, 4], ... index=pd.DatetimeIndex( ... ["2023-01-01", "2023-01-15", "2023-02-01", "2023-02-15"] ... ), ... ) >>> ser 2023-01-01 1 2023-01-15 2 2023-02-01 3 2023-02-15 4 dtype: int64
Example for
ffillwith downsampling (we have fewer dates after resampling):>>> ser.resample("MS").ffill() 2023-01-01 1 2023-02-01 3 Freq: MS, dtype: int64
Example for
ffillwith upsampling (fill the new dates with the previous value):>>> ser.resample("W").ffill() 2023-01-01 1 2023-01-08 1 2023-01-15 2 2023-01-22 2 2023-01-29 2 2023-02-05 3 2023-02-12 3 2023-02-19 4 Freq: W-SUN, dtype: int64
With upsampling and limiting (only fill the first new date with the previous value):
>>> ser.resample("W").ffill(limit=1) 2023-01-01 1.0 2023-01-08 1.0 2023-01-15 2.0 2023-01-22 2.0 2023-01-29 NaN 2023-02-05 3.0 2023-02-12 NaN 2023-02-19 4.0 Freq: W-SUN, dtype: float64