티스토리 뷰

Pandas.Series.rolling




Series.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)(0.23.4)

Provides rolling window calculation

  • sum

  • mean

  • std

  • ...

Example

 import datetime
 import pandas_datareader as web
 
 start = datetime.datetime(2010,1,1)
 end = datetime.datetime(2016,12,31)
 
 #HyunDae
 hd = web.DataReader('005380.KS', 'yahoo', start, end)
 hd_close = hd['Close']
 
 print(he_close.haed(5))

 '''
 Date
 2010-01-04   119000.0
 2010-01-05   110000.0
 2010-01-06   111000.0
 2010-01-07   106000.0
 2010-01-08   106000.0
 '''

 print(hd_close.rolling(window=5).mean()[5])
 #106900.0
 print(he_close[:5].values.mean())
 #106900.0
 
 print(hd_close.rolling(window=5).mean())
 '''
 Date
 2010-01-04         NaN
 2010-01-05         NaN
 2010-01-06         NaN
 2010-01-07         NaN
 2010-01-08   110400.0
 2010-01-11   106900.0
 ...
 '''
 
 print(hd_close.rolling(window=5, center=True).mean())
 #window size가 짝수 일 때 -> center : 올림하여 계산
 '''
 2010-01-04         NaN
 2010-01-05         NaN
 2010-01-06   110400.0
 ...
 2016-12-27   144500.0
 2016-12-28         NaN
 2016-12-29         NaN
 '''
 
 #hd_close.rolling(window=12).std()
 #hd_close.rolling(window=12).sum()


 **reference**
 
 http://ordo.tistory.com/67
 
 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.rolling.html

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함