# Buggy code
def set_index(self, col_or_cols, drop=True, inplace=False):
    """
    Set the DataFrame index (row labels) using one or more existing
    columns. By default yields a new object.

    Parameters
    ----------
    col_or_cols : column label or list of column labels
    drop : boolean, default True
        Delete columns to be used as the new index
    inplace : boolean, default False
        Modify the DataFrame in place (do not create a new object)

    Returns
    -------
    dataframe : DataFrame
    """
    cols = col_or_cols
    if not isinstance(col_or_cols, (list, tuple)):
        cols = [col_or_cols]

    if inplace:
        frame = self

    else:
        frame = self.copy()

    arrays = []
    for col in cols:
        level = frame[col]
        if drop:
            del frame[col]
        arrays.append(level)

    index = MultiIndex.from_arrays(arrays, names=cols)

    if not index._verify_integrity():
        duplicates = index._get_duplicates()
        raise Exception("Index has duplicate keys: %s" % duplicates)

    frame.index = index

    return frame


# Traceback (AssertionError)
df.as_matrix()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/frame.py", line 671, in as_matrix
self._consolidate_inplace()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/generic.py", line 267, in _consolidate_inplace
self._data = self._data.consolidate()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 516, in consolidate
new_blocks = _consolidate(self.blocks, self.items)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 938, in _consolidate
new_block = _merge_blocks(list(group_blocks), items)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 951, in _merge_blocks
do_integrity_check=True)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 211, in make_block
do_integrity_check=do_integrity_check)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 34, in __init__
self._check_integrity()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 40, in _check_integrity
return (self.ref_locs[1:] > self.ref_locs[:-1]).all()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pandas/core/internals.py", line 47, in ref_locs
assert((indexer != -1).all())
AssertionError

# Fixed code
def set_index(self, col_or_cols, drop=True, inplace=False):
    """
    Set the DataFrame index (row labels) using one or more existing
    columns. By default yields a new object.

    Parameters
    ----------
    col_or_cols : column label or list of column labels
    drop : boolean, default True
        Delete columns to be used as the new index
    inplace : boolean, default False
        Modify the DataFrame in place (do not create a new object)

    Returns
    -------
    dataframe : DataFrame
    """
    cols = col_or_cols
    if not isinstance(col_or_cols, (list, tuple)):
        cols = [col_or_cols]

    if inplace:
        frame = self

    else:
        frame = self.copy()

    arrays = []
    for col in cols:
        level = frame[col]
        if drop:
            del frame[col]
        arrays.append(level)

    index = MultiIndex.from_arrays(arrays, names=cols)

    if not index._verify_integrity():
        duplicates = index._get_duplicates()
        raise Exception("Index has duplicate keys: %s" % duplicates)

    # clear up memory usage
    index._cleanup()

    frame.index = index
    return frame