Repository: ydataai/ydata-profiling

Issue:
ZeroDivisionError when trying to profile a dataframe with only columns of object dtype
An example:

```
>>> df = pd.DataFrame({'a': ['a', 'b']})
>>> pandas_profiling.ProfileReport(df)
/home/me/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py:2782: UserWarning: Attempting to set identical left==right results
in singular transformations; automatically expanding.
left=-0.5, right=-0.5
  'left=%s, right=%s') % (left, right))
/home/me/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py:3040: UserWarning: Attempting to set identical bottom==top results
in singular transformations; automatically expanding.
bottom=-0.5, top=-0.5
  'bottom=%s, top=%s') % (bottom, top))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/me/.local/lib/python3.6/site-packages/pandas_profiling-1.4.1-py3.6.egg/pandas_profiling/__init__.py", line 69, in __init__
    moved={'CParserError': 'pandas.errors.ParserError'})
  File "/home/me/.local/lib/python3.6/site-packages/pandas_profiling-1.4.1-py3.6.egg/pandas_profiling/report.py", line 199, in to_html
  File "/home/me/.local/lib/python3.6/site-packages/pandas_profiling-1.4.1-py3.6.egg/pandas_profiling/plot.py", line 136, in correlation_matrix

ZeroDivisionError: float division by zero
```
(EDIT: removed some personal information from the error message)

The error is due to pandas-profiling trying to divide by the length of an empty dataframe in the function that plots the correlations matrices. Should be easy to fix by omitting the correlation matrix plots if there is no numerical data present in the dataframe.


Discussion:
Hello,

Were you able to omit the correlation matrix plots, If so how were you able to omit them? Any suggestion would be greatly appreciated.

Regards,
Kishan
There is currently no way to omit them. I did so by manually hacking into the code.
I added a scalar column to the df that was a float.  It now works, but you probably want to turn the correlation off in this case.


Resolving patch:
diff --git a/pandas_profiling/report.py b/pandas_profiling/report.py
--- a/pandas_profiling/report.py
+++ b/pandas_profiling/report.py
@@ -134,6 +134,7 @@ def extreme_obs_table(freqtable, table_template, row_template, number_to_print,
     # Variables
     rows_html = u""
     messages = []
+    render_htmls = {}
 
     for idx, row in stats_object['variables'].iterrows():
 
@@ -179,6 +180,7 @@ def extreme_obs_table(freqtable, table_template, row_template, number_to_print,
             formatted_values['lastn_expanded'] = extreme_obs_table(stats_object['freq'][idx], templates.template('freq_table'), templates.template('freq_table_row'), 5, n_obs, ascending = False)
 
         rows_html += templates.row_templates_dict[row['type']].render(values=formatted_values, row_classes=row_classes)
+    render_htmls['rows_html'] = rows_html
 
     # Overview
     formatted_values = {k: fmt(v, k) for k, v in six.iteritems(stats_object['table'])}
@@ -194,19 +196,19 @@ def extreme_obs_table(freqtable, table_template, row_template, number_to_print,
         messages_html += templates.message_row.format(message=msg)
 
     overview_html = templates.template('overview').render(values=formatted_values, row_classes = row_classes, messages=messages_html)
+    render_htmls['overview_html'] = overview_html
 
-    # Add plot of matrix correlation
-    pearson_matrix = plot.correlation_matrix(stats_object['correlations']['pearson'], 'Pearson')
-    spearman_matrix = plot.correlation_matrix(stats_object['correlations']['spearman'], 'Spearman')
-    correlations_html = templates.template('correlations').render(
-        values={'pearson_matrix': pearson_matrix, 'spearman_matrix': spearman_matrix})
+    # Add plot of matrix correlation if the dataframe is not empty
+    if len(stats_object['correlations']['pearson']) > 0:
+        pearson_matrix = plot.correlation_matrix(stats_object['correlations']['pearson'], 'Pearson')
+        spearman_matrix = plot.correlation_matrix(stats_object['correlations']['spearman'], 'Spearman')
+        correlations_html = templates.template('correlations').render(
+            values={'pearson_matrix': pearson_matrix, 'spearman_matrix': spearman_matrix})
+        render_htmls['correlations_html'] = correlations_html
 
     # Add sample
     sample_html = templates.template('sample').render(sample_table_html=sample.to_html(classes="sample"))
+    render_htmls['sample_html'] = sample_html
+
     # TODO: should be done in the template
-    return templates.template('base').render({
-        'overview_html': overview_html,
-        'rows_html': rows_html,
-        'sample_html': sample_html,
-        'correlation_html': correlations_html
-    })
+    return templates.template('base').render(render_htmls)


Test patch: