# Buggy code
def to_hierarchical(self, n_repeat, n_shuffle=1):
    """
    Return a MultiIndex reshaped to conform to the
    shapes given by n_repeat and n_shuffle.

    Useful to replicate and rearrange a MultiIndex for combination
    with another Index with n_repeat items.

    Parameters
    ----------
    n_repeat : int
        Number of times to repeat the labels on self
    n_shuffle : int
        Controls the reordering of the labels. If the result is going
        to be an inner level in a MultiIndex, n_shuffle will need to be
        greater than one. The size of each label must divisible by
        n_shuffle.

    Returns
    -------
    MultiIndex

    Examples
    --------
    >>> idx = MultiIndex.from_tuples([(1, u'one'), (1, u'two'),
                                      (2, u'one'), (2, u'two')])
    >>> idx.to_hierarchical(3)
    MultiIndex(levels=[[1, 2], [u'one', u'two']],
               labels=[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
                       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]])
    """
    levels = self.levels
    labels = [np.repeat(x, n_repeat) for x in self.labels]
    # Assumes that each label is divisible by n_shuffle
    labels = [x.reshape(n_shuffle, -1).ravel(1) for x in labels]
    names = self.names
    return MultiIndex(levels=levels, labels=labels, names=names)


# Traceback (IndexError)
isinstance(n, int)
True
df.loc[(n, 0), 'dest']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/pandas/core/indexing.py", line 1196, in __getitem__
return self._getitem_tuple(key)
File "/usr/local/lib/python3.5/site-packages/pandas/core/indexing.py", line 709, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "/usr/local/lib/python3.5/site-packages/pandas/core/indexing.py", line 817, in _getitem_lowerdim
return self._getitem_nested_tuple(tup)
File "/usr/local/lib/python3.5/site-packages/pandas/core/indexing.py", line 889, in _getitem_nested_tuple
obj = getattr(obj, self.name)._getitem_axis(key, axis=axis)
File "/usr/local/lib/python3.5/site-packages/pandas/core/indexing.py", line 1343, in _getitem_axis
return self._get_label(key, axis=axis)
File "/usr/local/lib/python3.5/site-packages/pandas/core/indexing.py", line 86, in _get_label
return self.obj._xs(label, axis=axis)
File "/usr/local/lib/python3.5/site-packages/pandas/core/generic.py", line 1483, in xs
drop_level=drop_level)
File "/usr/local/lib/python3.5/site-packages/pandas/core/index.py", line 5432, in get_loc_level
return (self._engine.get_loc(_values_from_object(key)),
File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:3979)
File "pandas/index.pyx", line 146, in pandas.index.IndexEngine.get_loc (pandas/index.c:3693)
File "pandas/src/util.pxd", line 41, in util.get_value_at (pandas/index.c:13199)
IndexError: index out of bounds

# Fixed code
def to_hierarchical(self, n_repeat, n_shuffle=1):
    """
    Return a MultiIndex reshaped to conform to the
    shapes given by n_repeat and n_shuffle.

    Useful to replicate and rearrange a MultiIndex for combination
    with another Index with n_repeat items.

    Parameters
    ----------
    n_repeat : int
        Number of times to repeat the labels on self
    n_shuffle : int
        Controls the reordering of the labels. If the result is going
        to be an inner level in a MultiIndex, n_shuffle will need to be
        greater than one. The size of each label must divisible by
        n_shuffle.

    Returns
    -------
    MultiIndex

    Examples
    --------
    >>> idx = MultiIndex.from_tuples([(1, u'one'), (1, u'two'),
                                      (2, u'one'), (2, u'two')])
    >>> idx.to_hierarchical(3)
    MultiIndex(levels=[[1, 2], [u'one', u'two']],
               labels=[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
                       [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]])
    """
    levels = self.levels
    labels = [np.repeat(x, n_repeat) for x in self.labels]
    # Assumes that each label is divisible by n_shuffle
    labels = [x.reshape(n_shuffle, -1).ravel("F") for x in labels]
    names = self.names
    return MultiIndex(levels=levels, labels=labels, names=names)