Python时间序列EMA计算报错求助:list对象无ewm/ewma属性
Let's break down why you're hitting this error and how to fix it quickly:
Root Cause
The error 'list' object has no attribute 'ewm' happens because your dataset a is a native Python list—but the ewm() method is exclusive to pandas objects like Series or DataFrame. Lists don't have this built-in method.
Step-by-Step Solution
Verify your data type first (to confirm our diagnosis):
print(type(a))If the output is
<class 'list'>, that's exactly the issue.Convert your list to a pandas Series/DataFrame:
Since you're working with time-series data (which is typically 1-dimensional), converting to aSeriesis the right call:import pandas as pd # Convert list to pandas Series a_series = pd.Series(a)Calculate Exponential Moving Average correctly:
Now you can use the updatedewm().mean()method with your pandas Series:expwavg = a_series.ewm(span=12, adjust=True).mean() plt.plot(a_series) plt.plot(expwavg, color='red')
Quick Note on the API Change
You're right about the pandas API update—old methods like pd.ewma() have been deprecated in favor of the more flexible ewm().mean() chain. Your approach to using the new method was correct; you just missed converting your list to a pandas-compatible object first.
内容的提问来源于stack exchange,提问作者brooklinite81

