# Buggy code
def _normalize_dataframe(dataframe, index):
    """Take a pandas DataFrame and count the element present in the
    given columns, return a hierarchical index on those columns
    """
    # groupby the given keys, extract the same columns and count the element
    # then collapse them with a mean
    data = dataframe[index].dropna()
    grouped = data.groupby(index, sort=False)
    counted = grouped[index].count()
    averaged = counted.mean(axis=1)
    return averaged


# Traceback (ValueError)
/home/nbuser/anaconda3_501/lib/python3.6/site-packages/statsmodels/graphics/mosaicplot.py:40: RuntimeWarning: invalid value encountered in less
if np.any(proportion < 0):
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
/home/nbuser/anaconda3_501/lib/python3.6/site-packages/matplotlib/transforms.py:402: RuntimeWarning: invalid value encountered in double_scalars
return points[1, 0] - points[0, 0]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3_501/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
339                 pass
340             else:
--> 341                 return printer(obj)
342             # Finally look for special method names
343             method = get_real_method(obj, self.print_method)

~/anaconda3_501/lib/python3.6/site-packages/IPython/core/pylabtools.py in <lambda>(fig)
242
243     if 'png' in formats:
--> 244         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
245     if 'retina' in formats or 'png2x' in formats:
246         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~/anaconda3_501/lib/python3.6/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
126
127     bytes_io = BytesIO()
--> 128     fig.canvas.print_figure(bytes_io, **kw)
129     data = bytes_io.getvalue()
130     if fmt == 'svg':

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2051                     bbox_artists = kwargs.pop("bbox_extra_artists", None)
2052                     bbox_inches = self.figure.get_tightbbox(renderer,
-> 2053                             bbox_extra_artists=bbox_artists)
2054                     pad = kwargs.pop("pad_inches", None)
2055                     if pad is None:

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/figure.py in get_tightbbox(self, renderer, bbox_extra_artists)
2271
2272         for a in artists:
-> 2273             bbox = a.get_tightbbox(renderer)
2274             if bbox is not None and (bbox.width != 0 or bbox.height != 0):
2275                 bb.append(bbox)

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/axes/_base.py in get_tightbbox(self, renderer, call_axes_locator, bbox_extra_artists)
4220
4221         for a in bbox_artists:
-> 4222             bbox = a.get_tightbbox(renderer)
4223             if bbox is not None and (bbox.width != 0 or bbox.height != 0):
4224                 bb.append(bbox)

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/artist.py in get_tightbbox(self, renderer)
269         """
270
--> 271         bbox = self.get_window_extent(renderer)
272         if self.get_clip_on():
273             clip_box = self.get_clip_box()

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/spines.py in get_window_extent(self, renderer)
151         # correct:
152         self._adjust_location()
--> 153         return super().get_window_extent(renderer=renderer)
154
155     def get_path(self):

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/patches.py in get_window_extent(self, renderer)
546
547     def get_window_extent(self, renderer=None):
--> 548         return self.get_path().get_extents(self.get_transform())
549
550

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/path.py in get_extents(self, transform)
526         path = self
527         if transform is not None:
--> 528             transform = transform.frozen()
529             if not transform.is_affine:
530                 path = self.transformed(transform)

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/transforms.py in frozen(self)
2191
2192     def frozen(self):
-> 2193         return blended_transform_factory(self._x.frozen(), self._y.frozen())
2194     frozen.__doc__ = Transform.frozen.__doc__
2195

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/transforms.py in blended_transform_factory(x_transform, y_transform)
2338     if (isinstance(x_transform, Affine2DBase)
2339         and isinstance(y_transform, Affine2DBase)):
-> 2340         return BlendedAffine2D(x_transform, y_transform)
2341     return BlendedGenericTransform(x_transform, y_transform)
2342

~/anaconda3_501/lib/python3.6/site-packages/matplotlib/transforms.py in __init__(self, x_transform, y_transform, **kwargs)
2279         is_correct = is_affine and is_separable
2280         if not is_correct:
-> 2281             raise ValueError("Both *x_transform* and *y_transform* must be 2D "
2282                              "affine transforms")
2283

ValueError: Both *x_transform* and *y_transform* must be 2D affine transforms

# Fixed code
def _normalize_dataframe(dataframe, index):
    """Take a pandas DataFrame and count the element present in the
    given columns, return a hierarchical index on those columns
    """
    # groupby the given keys, extract the same columns and count the element
    # then collapse them with a mean
    data = dataframe[index].dropna()
    grouped = data.groupby(index, sort=False)
    counted = grouped[index].count()
    averaged = counted.mean(axis=1)
    # Fill empty missing with 0, see GH5639
    averaged = averaged.fillna(0.0)
    return averaged