def TS_AVERAGE(series, number): number = int(number) l = len(series) shift = series for k in range(0, number-1): shift = shift.shift(1) series = series + shift series = series / number return series def TS_wgdStd(series, number, halflife): halflife = int(halflife) d = math.pow(0.5, 1 / halflife) DecayWGT = np.logspace(0, number - 1, number, base=d) avg = TS_AVERAGE(series, number) square = (series - avg) * (series - avg) print(square) result = math.sqrt(np.average(square, weights=DecayWGT)) return result testseq = range(0, 400) testseq = pd.Series(testseq) print(testseq) print('测试均值函数') print(TS_AVERAGE(testseq.values, 20)) TS_AVERAGE(testseq.values, 20).to_csv('测试均值函数.csv') print('测试加权标准差') print(TS_wgdStd(testseq.values, number=20, halflife=30)) TS_wgdStd(testseq.values, 20, 30).to_csv('测试加权标准差.csv')
时间: 2024-04-15 15:29:41 浏览: 150
根据你提供的代码,我注意到你正在尝试在 `TS_AVERAGE` 函数中使用 `shift` 方法。然而,在调用 `shift` 方法之前,你需要将 `series` 转换为 pandas Series 对象。
以下是修正后的代码:
```python
import pandas as pd
import math
import numpy as np
def TS_AVERAGE(series, number):
number = int(number)
l = len(series)
shift = pd.Series(series)
for k in range(0, number-1):
shift = shift.shift(1)
series = series + shift
series = series / number
return series
def TS_wgdStd(series, number, halflife):
halflife = int(halflife)
d = math.pow(0.5, 1 / halflife)
DecayWGT = np.logspace(0, number - 1, number, base=d)
avg = TS_AVERAGE(series, number)
square = (series - avg) * (series - avg)
print(square)
result = math.sqrt(np.average(square, weights=DecayWGT))
return result
testseq = range(0, 400)
testseq = pd.Series(testseq)
print(testseq)
print('测试均值函数')
print(TS_AVERAGE(testseq.values, 20))
TS_AVERAGE(testseq.values, 20).to_csv('测试均值函数.csv')
print('测试加权标准差')
print(TS_wgdStd(testseq.values, number=20, halflife=30))
TS_wgdStd(testseq.values, 20, 30).to_csv('测试加权标准差.csv')
```
在修正后的代码中,我们使用 `pd.Series` 将 `series` 转换为 pandas Series 对象,并将其赋值给 `shift` 变量。这样,在循环中调用 `shift` 方法时就不会引发错误。
请注意,我只修正了与 `shift` 方法相关的错误。如果还有其他问题,请提供相关的错误信息,以便我能够更好地帮助你解决问题。
阅读全文