'“.. in pandas..html.py .. self.fmt.col_space.items()”: AttributeError: 'NoneType' object has no attribute 'items'

I'm trying to find out error in a word similarity calculation.

def word_similarity_error_analysis(eval_df):
    eval_df['distance_rank'] = _normalized_ranking(eval_df['distance'])
    eval_df['score_rank'] = _normalized_ranking(eval_df['score'])
    eval_df['error'] =  abs(eval_df['distance_rank'] - eval_df['score_rank'])
    return eval_df.sort_values('error')


def _normalized_ranking(series):
    ranks = series.rank(method='dense')
    return ranks / ranks.sum()

word_similarity_error_analysis(eval_df).head()

And I'm getting this error:

AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    336             method = get_real_method(obj, self.print_method)
    337             if method is not None:
--> 338                 return method()
    339             return None
    340         else:

2 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in _repr_html_(self)
    732             return buf.getvalue()
    733 
--> 734         max_rows = get_option("display.max_rows")
    735         min_rows = get_option("display.min_rows")
    736         max_cols = get_option("display.max_columns")

/usr/local/lib/python3.6/dist-packages/pandas/io/formats/format.py in to_html(self, buf, encoding, classes, notebook, border)
    980             Whether the generated HTML is for IPython Notebook.
    981         border : int
--> 982             A ``border=border`` attribute is included in the opening
    983             ``<table>`` tag. Default ``pd.options.display.html.border``.
    984         """

/usr/local/lib/python3.6/dist-packages/pandas/io/formats/html.py in __init__(self, formatter, classes, border)
     57         self.col_space = {
     58             column: f"{value}px" if isinstance(value, int) else value
---> 59             for column, value in self.fmt.col_space.items()
     60         }
     61 

AttributeError: 'NoneType' object has no attribute 'items'



            word1    word2  score  ...  distance_rank  score_rank         error
1041  hummingbird  pelican  -32.0  ...       0.000243    0.000244  2.434543e-07
2315         lily     pigs  -13.0  ...       0.000488    0.000487  4.016842e-07
2951       bucket    girls   -4.0  ...       0.000602    0.000603  4.151568e-07
150         night   sunset  -43.0  ...       0.000102    0.000103  6.520315e-07
2062          oak   petals  -17.0  ...       0.000435    0.000436  7.162632e-07

[5 rows x 7 columns]

I saw many people faced the same type of error. However, can't find a suitable solution. What's wrong with this code? Also, how a result is getting produced after the error. The exact line number isn't visible where error is occurring.

(I'm using Google Colab and Pandas version 1.0.5 in case you need it)



Solution 1:[1]

I'm encountering the same issue on an AWS SageMaker notebook (Python3.6), with relatively standard Pandas code. I have a growing suspicion that this phenomenon is related to python-pandas version mismatches, because the same code on a local notebook with Python3.7 does not replicate the exception.

I have yet to validate this is definitely the issue because the environments are not quite identical, but try to run the same code with the same data on python > 3.6.

Solution 2:[2]

Up(down)grading to pandas==1.3.3 fixed this for me when running under Jupyter on AWS Sagemaker as at February 2022 - from the notebook:

!pip install pandas==1.3.3

Pandas 1.3.5 didn't work for me.

I don't know whether it's a version mismatch or a regression. Finally solved for me at least! :)

Solution 3:[3]

Had the same problem. Furthermore, also used Jupyter with pandas.print(df) worked fine, but df only returned an exception. After update of conda package problem disappeared.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nis_0
Solution 2 jtlz2
Solution 3 jtlz2