AttributeError: module 'pandas.compat' has no attribute 'iteritems'

Viewed 4010

I am trying to import pandas-ml but I get this import error. What might be the issue?

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-fd3c3c034481> in <module>
----> 1 import pandas_ml as pml

AttributeError: module 'pandas.compat' has no attribute 'iteritems' ```


3 Answers
File "/usr/local/lib/python3.7/site-packages/pandas_ml/core/accessor.py", line 81, in _update_method_mapper
    for key, class_dict in compat.iteritems(cls._method_mapper):
AttributeError: module 'pandas.compat' has no attribute 'iteritems'

I have the same error too with python3.7. I solved it with changing iteritems() to items() .

There are two lines on accessor.py under @classmethod, change them into:

 for key, class_dict in cls._method_mapper.items():
'
'
'
      class_dict = {k: getattr(cls, m) for k, m in class_dict.items()}

For my version, I have also encounter another import error ImportError: cannot import name 'range' from 'pandas.compat'in File "/usr/local/lib/python3.7/site-packages/pandas_ml/confusion_matrix/stats.py". just delete from pandas.compat import range would do.

Reference:

https://github.com/pandas-dev/pandas/commit/e26e2dfe6e93922830fb5fb7868b87238b85911a#diff-21f71fbdb0d3dfa55dc948e2ddcddc92

The iteritems attribute for pandas.compat appears to be have been removed recently, as seen here (tip from this source).

In other words, your current version of pandas is incompatible with pandas-ml currently.

The GitHub issue suggests to maybe downgrade your pandas version.

# Installed using pip
pip install pandas==0.24.2

# Installed using conda
conda install pandas==0.24.2

You can run the following in the Python REPL to double check the pandas package version to see if it is greater than 0.25.0.

import pandas

print(pandas.__version__)
Related