Module ‘pandas’ has no attribute ‘rolling_mean’ ( Solved )


Pandas is a python package that allows you to create dataframe and manipulate your datasets. It has many inbuilt functions that allow you to manipulate dataframes. But sometimes you can get an attribute errors while using some function. If you are getting module ‘pandas’ has no attribute ‘rolling_mean’ then this post is for you. Here you will know why this error comes and how to solve it.

What is an AttributeError?

In any python modules, there are functions that are inbuilt with it. Without knowing if there is any function included in that package or not, we call in our code. And when we run it then we get the AttributeError on that function.

The other case when you will get the attributerror is when you are passing the invalid variable type to the function arguments. For example python append() method accepts a list as an argument but if you will pass the string variable then you will get the error.

Cause for the module ‘pandas’ has no attribute ‘rolling_mean’

If you are getting the error module ‘pandas’ has no attribute ‘rolling_mean’ then it is obvious that you are using the wrong function provided by the python pandas package. The dataframe.rolling_mean() method is now deprecated by the pandas for calculating the mean of the rows.

You will get the error when you will execute the below lines of code.

import pandas as pd
data = {"col1":[10,20,30,40,50,40,30,20,10,0]}
df = pd.DataFrame(data)
ma = pd.rolling_mean(df,5)

Output

module 'pandas' has no attribute 'rolling_mean' error
module ‘pandas’ has no attribute ‘rolling_mean’ error

The solution for the module ‘pandas’ has no attribute ‘rolling_mean’

The solution for this error is that you have to update the previous version of the pandas module. The method pd.rolling_mean() is not provided in the current version. To solve this error you have to install the new version of the pandas package or upgrade to the latest version.

You can use the below command to upgrade the panads package.

pip3 install -U pandas

You have to use the df.rolling().mean() method to find the mean for your dataframe.

Use the below lines of code to find the mean of the data for window_size of 5.

import pandas as pd
data = {"col1":[10,20,30,40,50,40,30,20,10,0]}
df = pd.DataFrame(data)
ma = df.rolling(window=5).mean()
ma

Output

Finding rolling mean of the dataframe
Finding the rolling mean of the dataframe

Now you can see you are not getting any errors.

Conclusion

Pandas is a great library for data manipulation. If you have experience with it then you can face this type of error when you are unaware of function changes. Therefore you must keep updated with the new version of any packages.

The above method will solve the error module ‘pandas’ has no attribute ‘rolling_mean’.

I hope you have liked this tutoril. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.





Source link