How to Solve Python AttributeError: module ‘pandas’ has no attribute ‘rolling_mean’

by | Pandas, Programming, Python, Tips

In Pandas 0.18.0 and above, window functions like rolling_mean were refactored into methods on Series/DataFrame objects rather than top-level functions. The function rolling_mean is deprecated in Pandas version 0.18.0 and above. Instead of pd.rolling_mean(dataframe, window=5), you should use dataframe.rolling(window=5).mean().

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: module ‘pandas’ has no attribute ‘rolling_mean’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part of the error ‘pandas’ has no attribute ‘rolling_mean‘ tells us that the pandas class does not have the rolling_means method as an attribute. The rolling_mean() method, along with other window-based methods, is deprecated as of version 0.18.0, so if you are using a version after 0.17.0, you will get the AttributeError.

Example

Let’s look at an example where we want to calculate the rolling average on a dataset containing the monthly volume of water in a pond in litres. First, we will create the dataset. The first column contains the monthly period, and the second column contains the amount of water in the pond for each month in litres.

import numpy as np 
import pandas as pd

np.random.seed(0)

period = np.arange(1, 101, 1)
litres = np.random.uniform(1, 20, 100)
df = pd.DataFrame({'period':period, 'litres':litres})

print(df.head(10))

Let’s print the first ten rows of the DataFrame:

   period     litres
0       1  11.427457
1       2  14.588598
2       3  12.452504
3       4  11.352780
4       5   9.049441
5       6  13.271988
6       7   9.314157
7       8  17.943687
8       9  19.309592
9      10   8.285389

Let’s try to calculate the rolling mean on the litres of water using rolling_mean with a window of 5.

df['rolling_litres'] = pd.rolling_mean(df['litres'], 5)
print(df['rolling_litres'])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 df['rolling_litres'] = pd.rolling_mean(df['litres'], 5)
      2 print(df['rolling_litres'])

File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/__init__.py:261, in __getattr__(name)
    257     from pandas.core.arrays.sparse import SparseArray as _SparseArray
    259     return _SparseArray
--> 261 raise AttributeError(f"module 'pandas' has no attribute '{name}'")

AttributeError: module 'pandas' has no attribute 'rolling_mean'

The error occurs because rolling_mean is a deprecated method as of Pandas 0.18.0.

print(f'Pandas version: {pd.__version__}')
Pandas version: 1.4.1

Solution

To solve this error, we need to use the rolling instead of pd.rolling_mean. Let’s look at the .rolling API:

r = df.rolling(window=5)

Now that we have an object of the rolling class, we can use the dir() function to list the available methods and properties:

print(dir(r))
['__annotations__', '__class__', '__class_getitem__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__orig_bases__', '__parameters__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_apply', '_apply_blockwise', '_apply_pairwise', '_apply_series', '_apply_tablewise', '_attributes', '_check_window_bounds', '_create_data', '_dir_additions', '_generate_cython_apply_func', '_get_window_indexer', '_gotitem', '_index_array', '_insert_on_column', '_internal_names', '_internal_names_set', '_is_protocol', '_numba_apply', '_obj_with_exclusions', '_on', '_prep_values', '_raise_monotonic_error', '_resolve_output', '_selected_obj', '_selection', '_selection_list', '_validate', '_validate_monotonic', '_win_freq_i8', '_win_type', 'agg', 'aggregate', 'apply', 'axis', 'center', 'closed', 'corr', 'count', 'cov', 'exclusions', 'is_datetimelike', 'kurt', 'max', 'mean', 'median', 'method', 'min', 'min_periods', 'ndim', 'obj', 'on', 'quantile', 'rank', 'sem', 'skew', 'std', 'sum', 'validate', 'var', 'win_type', 'window']

We can see that mean() is an available method. The methods operate on the rolling object itself, not the Series. Let’s look at the revised code:

df['rolling_litres'] = df['litres'].rolling(5).mean()
print(df.head(10))

We will print the first ten rows of the updated DataFrame. Let’s run the code to see the result:

  period     litres  rolling_litres
0       1  11.427457             NaN
1       2  14.588598             NaN
2       3  12.452504             NaN
3       4  11.352780             NaN
4       5   9.049441       11.774156
5       6  13.271988       12.143062
6       7   9.314157       11.088174
7       8  17.943687       12.186411
8       9  19.309592       13.777773
9      10   8.285389       13.624963

We successfully calculated the rolling mean on the values in the litres column using a window size of five.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: module ‘pandas’ has no attribute ‘rolling_mean’ occurs when you try to use the deprecated rolling_mean method. You can access window-based calculations through the rolling class.

For further reading on Pandas AttributeErrors, go to the article: How to Solve Python AttributeError: module ‘pandas’ has no attribute ‘dataframe’.

For further reading on Pandas, go to the article: Introduction to Pandas: A Complete Tutorial for Beginners.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee