解决方法
在rolling()
方法中添加参数min_periods
即可
示例
import pandas as pd
import numpy as np
if __name__ == '__main__':
data = np.random.randint(0, 10, size=(20))
series = pd.Series(data)
series1 = series.rolling(3).mean()
series2 = series.rolling(3, min_periods=1).mean()
print(series1) # 前2个数是nan
print(series2) # 会遇到多少个数,就用多少个数