python時間序列2

2021-09-11 06:05:39 字數 1160 閱讀 5805

#錨定位置

from pandas.tseries.offsets import day,monthend

now=datetime.datetime(2011,11,17)

# 錨定偏置量:monthend

print(now+3*day())

# monthend將now這個日期移至本月末尾;如果日期已經是本月末尾了,就移至下月末尾算1

print(now+monthend())

print(now+monthend(2))

print(now+monthend(3))

結果輸出:

2011-11-20 00:00:00

2011-11-30 00:00:00

2011-12-31 00:00:00

2012-01-31 00:00:00

#錨定位置還可以使用rollforward和rollback分別顯示地將日期向前或者向後「滾動」

offset=monthend()

oo=offset.rollforward(now)

print(oo)

bb=offset.rollback(now)

print(bb)

應用場景:

月銷售額統計,根據月份錨定位置,分組求和求平均

例子

from pandas.tseries.offset import day,monthend

offset=monthend()

ts=pd.series(np.random.randn(20),index=pd.date_range('1/15/2000',periods=20,freq=4))

print(ts)

g=ts.groupby(offset.rollforward).mean()

print(g)

g:輸出結果

2000-01-31 -0.493799

2000-02-29 -0.028332

2000-03-31 0.328148

dtype: float64

#這個更容易

print(ts.resample('m').mean())

時間序列python

平穩性檢測 平穩性的定義 圍繞乙個常數上下波動且波動範圍有限,即有常數均值和常數方差。如果有明顯的趨勢或者週期性,那它通常不是平穩序列。檢測方法有三種 1 時序圖檢測 2 自相關係數和偏相關係數 通過spss 截尾 就是在某階之後,係數都為0 拖尾 就是有乙個緩慢衰減的趨勢,但是不都為0 2.不平穩...

python時間序列

from datetime import datetime 返回當前時刻的日期和時間 print datetime.now 分別返回當前時刻的年,月,日 print datetime.now year print datetime.now month print datetime.now day 返...

python時間序列分析

什麼是時間序列 時間序列簡單的說就是各時間點上形成的數值串行,時間序列分析就是通過觀察歷史資料 未來的值。在這裡需要強調一點的是,時間序列分析並不是關於時間的回歸,它主要是研究自身的變化規律的 這裡不考慮含外生變數的時間序列 為什麼用python 用兩個字總結 情懷 愛屋及烏,個人比較喜歡pytho...