我有一个包含以下值的csv文件:
Date Value
15/01/2016 0.6972
14/01/2016 0.6985
13/01/2016 0.6955
12/01/2016 0.6986
11/01/2016 0.6995
8/01/2016 0.6953
这是一个超过5年的日常数据的时间序列。如何访问pandas来创建一个ewma(10)(指数跨度为10的指数加权移动平均线?
非常感谢!
答案 0 :(得分:0)
IIUC你可以尝试ewma
:
import pandas as pd
import numpy as np
import io
temp=u"""Date;Value
15/01/2016;0.6972
14/01/2016;0.6985
13/01/2016;0.6955
12/01/2016;0.6986
11/01/2016;0.6995
8/01/2016;0.6953"""
#change io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep=";", index_col=None, parse_dates=[0])
print df
Date Value
0 2016-01-15 0.6972
1 2016-01-14 0.6985
2 2016-01-13 0.6955
3 2016-12-01 0.6986
4 2016-11-01 0.6995
5 2016-08-01 0.6953
print pd.ewma(df['Value'], span=10)
0 0.697200
1 0.697915
2 0.696944
3 0.697490
4 0.698067
5 0.697348
Name: Value, dtype: float64
或类似的东西:
import pandas as pd
import io
temp=u"""Date;Value
15/01/2016;0.6972
14/01/2016;0.6985
13/01/2016;0.6955
12/01/2016;0.6986
11/01/2016;0.6995
8/01/2016;0.6953"""
#change io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep=";", index_col=[0], parse_dates=[0])
print df
Value
Date
2016-01-15 0.6972
2016-01-14 0.6985
2016-01-13 0.6955
2016-12-01 0.6986
2016-11-01 0.6995
2016-08-01 0.6953
print pd.ewma(df['Value'], span=10, freq='D')
Date
2016-01-13 0.695500
2016-01-14 0.697150
2016-01-15 0.697170
2016-01-16 0.697170
2016-01-17 0.697170
2016-01-18 0.697170
2016-01-19 0.697170
2016-01-20 0.697170
2016-01-21 0.697170
2016-01-22 0.697170
2016-01-23 0.697170
2016-01-24 0.697170
2016-01-25 0.697170
2016-01-26 0.697170
2016-01-27 0.697170
2016-01-28 0.697170
2016-01-29 0.697170
2016-01-30 0.697170
2016-01-31 0.697170
2016-02-01 0.697170
2016-02-02 0.697170
2016-02-03 0.697170
2016-02-04 0.697170
2016-02-05 0.697170
2016-02-06 0.697170
2016-02-07 0.697170
2016-02-08 0.697170
2016-02-09 0.697170
2016-02-10 0.697170
2016-02-11 0.697170
...
2016-11-02 0.699500
2016-11-03 0.699500
2016-11-04 0.699500
2016-11-05 0.699500
2016-11-06 0.699500
2016-11-07 0.699500
2016-11-08 0.699500
2016-11-09 0.699500
2016-11-10 0.699500
2016-11-11 0.699500
2016-11-12 0.699500
2016-11-13 0.699500
2016-11-14 0.699500
2016-11-15 0.699500
2016-11-16 0.699500
2016-11-17 0.699500
2016-11-18 0.699500
2016-11-19 0.699500
2016-11-20 0.699500
2016-11-21 0.699500
2016-11-22 0.699500
2016-11-23 0.699500
2016-11-24 0.699500
2016-11-25 0.699500
2016-11-26 0.699500
2016-11-27 0.699500
2016-11-28 0.699500
2016-11-29 0.699500
2016-11-30 0.699500
2016-12-01 0.698602
Freq: D, Name: Value, dtype: float64