pandas.core.resample.Resampler.ffill#
- finalResampler.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.fillna
Fill NA/NaN values using the specified method.
DataFrame.fillna
Fill 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
ffill
with 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
ffill
with 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