Stacey
Stacey

Reputation: 5097

Pandas rolling mean - TypeError: Can't convert 'int' object to str implicitly

I have a dataframe 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

and am trying to create a moving average of this using:

df["B"] = pd.rolling_mean(df["A"] ,5)

But I get the error : TypeError: Can't convert 'int' object to str implicitly

Is this error due to the NaN on the first row of the Df or something else. The stack trace is as follows:

    \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

Upvotes: 0

Views: 154

Answers (1)

Andrew L
Andrew L

Reputation: 7038

It looks like you're working with a Series. Does the below work? NaNs should not interfere with this calculation:

df = df.to_frame()
df['b'] = df.rolling(5).mean()

Keep in mind I'm using your variable "df" where this is actually a Series (from what I see above). Also, Rolling_mean is deprecated so I'm wondering if perhaps this is causing issues.

Upvotes: 1

Related Questions