pandas.core.resample.Resampler.ffill#

final Resampler.ffill(limit=None)[源代码]#

向前填充值。

Parameters:
<strong>limit</strong>int, optional

要填充的最大值数量。

Returns:
一个向上采样后的 Series。

参见

Series.fillna

使用指定的方法填充NA/NaN值。

DataFrame.fillna

使用指定的方法填充NA/NaN值。

Examples

这里我们只创建一个 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

对下采样(重采样后日期变少)使用 ffill 的示例:

>>> ser.resample('MS').ffill()
2023-01-01    1
2023-02-01    3
Freq: MS, dtype: int64

对上采样使用 ``ffill``(用前一个值填充新日期)的示例:

>>> 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

使用上采样和限制(只用前一个值填充第一个新日期):

>>> 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