我有一个数据框(df
):
Name: BBG.XLON.VOD.S_MKTCAP_EUR, dtype: float64
date
2008-02-04 125761.886753
2008-02-05 123681.237732
2008-02-06 124062.436220
2008-02-07 121107.388396
2008-02-08 124216.990692
2008-02-11 122075.831250
2008-02-12 126868.359688
2008-02-13 124942.668750
2008-02-14 124220.030625
2008-02-15 121296.275000
2008-02-18 123004.414768
2008-02-19 119431.907487
2008-02-20 113577.349414
类型:datatype <class 'pandas.core.frame.DataFrame'>
我正在尝试计算我希望捕获的时间序列的指数加权移动平均值。
我使用以下代码:
df = pd.DataFrame({'': stockData[unique_id+"_MKTCAP_EUR"]})
McapResult = df.ewm(alpha=0.1, adjust=False).mean().iloc[-1]
McapResult
是指数系列中的最后一个值
但是我收到以下错误:
AttributeError:&#39; DataFrame&#39;对象没有属性&#39; ewm&#39;
有人可以让我知道我需要做些什么来解决这个问题吗?
答案 0 :(得分:2)
我认为您可以使用Series.ewm
而不是创建新DataFrame
df
- 结果相同:
import pandas as pd
stockData = pd.DataFrame({'BBG.XLON.VOD.S': {pd.Timestamp('2008-02-14 00:00:00'): 124220.030625, pd.Timestamp('2008-02-20 00:00:00'): 113577.34941400001, pd.Timestamp('2008-02-08 00:00:00'): 124216.99069200001, pd.Timestamp('2008-02-13 00:00:00'): 124942.66875, pd.Timestamp('2008-02-15 00:00:00'): 121296.27499999999, pd.Timestamp('2008-02-06 00:00:00'): 124062.43622, pd.Timestamp('2008-02-19 00:00:00'): 119431.90748699999, pd.Timestamp('2008-02-04 00:00:00'): 125761.886753, pd.Timestamp('2008-02-11 00:00:00'): 122075.83125, pd.Timestamp('2008-02-07 00:00:00'): 121107.38839600001, pd.Timestamp('2008-02-05 00:00:00'): 123681.23773199999, pd.Timestamp('2008-02-18 00:00:00'): 123004.414768, pd.Timestamp('2008-02-12 00:00:00'): 126868.35968800001}})
stockData.index.name = 'date'
print (stockData)
BBG.XLON.VOD.S
date
2008-02-04 125761.886753
2008-02-05 123681.237732
2008-02-06 124062.436220
2008-02-07 121107.388396
2008-02-08 124216.990692
2008-02-11 122075.831250
2008-02-12 126868.359688
2008-02-13 124942.668750
2008-02-14 124220.030625
2008-02-15 121296.275000
2008-02-18 123004.414768
2008-02-19 119431.907487
2008-02-20 113577.349414
unique_id = 'BBG.XLON.VOD.S'
McapResult = stockData[unique_id].ewm(alpha=0.1, adjust=False).mean().iloc[-1]
print (McapResult)
122786.42433
编辑:
我认为错误导致pandas的低级版本为0.18.0
,因为pandas.Series.ewm
为New in version 0.18.0
。