Pandas Time Series Sliding window

Source: Internet
Author: User

Time series data Statistics-sliding window window functions
import pandas as pdimport numpy as npser_obj = pd.Series(np.random.randn(1000),                     index=pd.date_range('20180101', periods=1000))ser_obj = ser_obj.cumsum()print(ser_obj.head())
2018-01-01    0.7973342018-01-02    0.4512862018-01-03    1.3291332018-01-04    0.4165772018-01-05    0.610993Freq: D, dtype: float64
r_obj = ser_obj.rolling(window=5)r_obj2 = ser_obj.rolling(window=5, center=True)print(r_obj)
Rolling [window=5,center=False,axis=0]
  print (R_obj2.mean ()) # Verify: # The mean value of the first 5 data # print (Ser_obj[0:5].mean ()) # 1-6 data mean # print (ser_obj [1:6].mean ())  
2018-01-01 nan2018-01-02 nan2018-01-03 0.7210652018-01-04 0.8293522018-01-05 0.6941212018-01     -06 0.2754952018-01-07 0.1492142018-01-08 0.4177342018-01-09 0.5204582018-01-10 1.0345062018-01-11 1.8124172018-01-12 2.4574102018-01-13 2.8099962018-01-14 3.0464432018-01-15 2.8382092018-01-16 2.4578 222018-01-17 2.1485082018-01-18 1.6478872018-01-19 1.0832202018-01-20 1.0135252018-01-21 0.9418502018-    01-22 0.7657512018-01-23 0.7035812018-01-24 0.7446162018-01-25 0.3017102018-01-26-0.1685972018-01-27    -0.8517262018-01-28-1.6212992018-01-29-2.5388152018-01-30-3.251647 ... 2020-08-28-50.5811432020-08-29-51.8263802020-08-30-52.9502752020-08-31-53.4123392020-09-01-53.8242062020-09 -02-54.0998402020-09-03-54.1402192020-09-04-54.2159372020-09-05-54.2428182020-09-06-53.9086752020-09-07- 53.4938512020-09-08-53.2099432020-09-09  -52.9427182020-09-10-53.0385472020-09-11-53.1880282020-09-12-53.7311452020-09-13-54.0918792020-09-14-54.8 671722020-09-15-55.2022942020-09-16-55.4405562020-09-17-54.9264392020-09-18-54.6196632020-09-19-54.12837620 20-09-20-54.2745262020-09-21-54.5274632020-09-22-55.3828802020-09-23-56.3091922020-09-24-57.4229082020-09-2 5 nan2020-09-26 nanfreq:d, length:1000, Dtype:float64
  print (R_obj2.mean ())  
2018-01-01 nan2018-01-02 nan2018-01-03 0.7210652018-01-04 0.8293522018-01-05 0.6941212018-01     -06 0.2754952018-01-07 0.1492142018-01-08 0.4177342018-01-09 0.5204582018-01-10 1.0345062018-01-11 1.8124172018-01-12 2.4574102018-01-13 2.8099962018-01-14 3.0464432018-01-15 2.8382092018-01-16 2.4578 222018-01-17 2.1485082018-01-18 1.6478872018-01-19 1.0832202018-01-20 1.0135252018-01-21 0.9418502018-    01-22 0.7657512018-01-23 0.7035812018-01-24 0.7446162018-01-25 0.3017102018-01-26-0.1685972018-01-27    -0.8517262018-01-28-1.6212992018-01-29-2.5388152018-01-30-3.251647 ... 2020-08-28-50.5811432020-08-29-51.8263802020-08-30-52.9502752020-08-31-53.4123392020-09-01-53.8242062020-09 -02-54.0998402020-09-03-54.1402192020-09-04-54.2159372020-09-05-54.2428182020-09-06-53.9086752020-09-07- 53.4938512020-09-08-53.2099432020-09-09  -52.9427182020-09-10-53.0385472020-09-11-53.1880282020-09-12-53.7311452020-09-13-54.0918792020-09-14-54.8 671722020-09-15-55.2022942020-09-16-55.4405562020-09-17-54.9264392020-09-18-54.6196632020-09-19-54.12837620 20-09-20-54.2745262020-09-21-54.5274632020-09-22-55.3828802020-09-23-56.3091922020-09-24-57.4229082020-09-2 5 nan2020-09-26 nanfreq:d, length:1000, Dtype:float64
# 画图查看import matplotlib.pyplot as plt%matplotlib inlineplt.figure(figsize=(15, 5))ser_obj.plot(style='r--')ser_obj.rolling(window=10, center=False).mean().plot(style='g')ser_obj.rolling(window=10, center=True).mean().plot(style='b')

Pandas Time Series Sliding window

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.