##窗口函数--cumsum()
import pandas as pd
import numpy as np
ser_obj = pd.Series(np.random.randn(1000),
index=pd.date_range('20170101', periods=1000))
ser_obj = ser_obj.cumsum()
print(ser_obj.head())
answer
2017-01-01 0.956250
2017-01-02 0.279960
2017-01-03 -0.629871
2017-01-04 0.797499
2017-01-05 0.557393
Freq: D, dtype: float64
r_obj = ser_obj.rolling(window=5)
print(r_obj)
answer
Rolling [window=5,center=False,axis=0] ####返回的是类对象,需要接统计函数才能展示
print(r_obj.mean().head())
answer:
2017-01-01 NaN
2017-01-02 NaN
2017-01-03 NaN
2017-01-04 NaN
2017-01-05 0.392246
2017-01-06 0.017798
2017-01-07 -0.175184
2017-01-08 -0.137657
2017-01-09 -0.209815
2017-01-10 -0.237701
Freq: D, dtype: float64
# 前5个数据的均值
print(ser_obj[0:5].mean())
answer
0.39224619085269624
# 1-6个数据的均值
print(ser_obj[1:6].mean())
answer:
0.017798268782202475
# 画图查看
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(15, 5))
ser_obj.plot(style='r--')
ser_obj.rolling(window=10).mean().plot(style='b')
print(ser_obj.rolling(window=5, center=True).mean())
answer:
2017-01-01 NaN
2017-01-02 NaN
2017-01-03 0.392246
2017-01-04 0.017798
2017-01-05 -0.175184
2017-01-06 -0.137657
2017-01-07 -0.209815
2017-01-08 -0.237701
2017-01-09 0.109869
2017-01-10 0.614796
2017-01-11 1.203267
2017-01-12 1.782855