我有一个数据帧df:
Name: A, dtype: float64
date
2001-01-02 NaN
2001-01-03 3.230186e-04
2001-01-04 4.315988e-05
2001-01-05 1.103871e-05
2015-03-30 5.063656e-05
2015-03-31 1.156415e-06
2015-04-01 2.037601e-05
2015-04-02 2.570277e-05
我正在尝试使用以下方法创建此移动平均线:
df["B"] = pd.rolling_mean(df["A"] ,5)
但我收到错误:TypeError: Can't convert 'int' object to str implicitly
这个错误是由于Df的第一行上的NaN还是其他原因造成的。堆栈跟踪如下:
\Users\stacey\Documents\scripts\A_1.1.py:315: FutureWarning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with
Series.rolling(center=False,window=5).mean()
df[name+"_RAWHISTVOLMATRIXMAV"+volMav1] = pd.rolling_mean(df[name+"_RAWHISTVOLMATRIX"] ,5)
Traceback (most recent call last):
File "C:\Users\stacey\Documents\scripts\A_Data_1.1.py", line 643, in <module>
main()
File "C:\Users\stacey\Documents\scripts\A_1.1.py", line 80, in main
stockData = getTimeseriesData(rawTimeseriesDataPath,1,startDate,endDate,volMav1,volMav2,volMav3,volMav4)
File "C:\Users\stacey\Documents\scripts\A_1.1.py", line 315, in getTimeseriesData
df["B"] = pd.rolling_mean(df["A"] ,5)
TypeError: Can't convert 'int' object to str implicitly
答案 0 :(得分:1)
看起来你正在使用一个系列。以下是否有效? NaN不应该干扰这个计算:
df = df.to_frame()
df['b'] = df.rolling(5).mean()
请记住我正在使用你的变量“df”,这实际上是一个系列(从我上面看到的)。此外,Rolling_mean已被弃用,因此我想知道这是否会导致问题。