# Buggy code
def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame":
    if columns is None:
        raise TypeError("pivot() missing 1 required argument: 'columns'")

    if values is None:
        cols = [columns] if index is None else [index, columns]
        append = index is None
        indexed = data.set_index(cols, append=append)
    else:
        if index is None:
            index = data.index
        else:
            index = data[index]
        index = MultiIndex.from_arrays([index, data[columns]])

        if is_list_like(values) and not isinstance(values, tuple):
            # Exclude tuple because it is seen as a single column name
            indexed = data._constructor(
                data[values].values, index=index, columns=values
            )
        else:
            indexed = data._constructor_sliced(data[values].values, index=index)
    return indexed.unstack(columns)


# Traceback (ValueError)
In [1]: df = pd.DataFrame({'lev1': [1, 1, 1, 1,2, 2, 2,2], 'lev2': [1, 1, 2, 2, 1, 1, 2, 2], 'lev3': [1, 2, 1, 2, 1, 2, 1, 2], 'values': range(8)})

In [2]: df
Out[2]:
lev1  lev2  lev3  values
0     1     1     1       0
1     1     1     2       1
2     1     2     1       2
3     1     2     2       3
4     2     1     1       4
5     2     1     2       5
6     2     2     1       6
7     2     2     2       7

In [3]: df.pivot(index=['lev1', 'lev2'], columns='lev3', values='values')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-2fef29f9fd39> in <module>()
----> 1 df.pivot(index=['lev1', 'lev2'], columns='lev3', values='values')

~/scipy/pandas/pandas/core/frame.py in pivot(self, index, columns, values)
5191         """
5192         from pandas.core.reshape.reshape import pivot
-> 5193         return pivot(self, index=index, columns=columns, values=values)
5194
5195     _shared_docs['pivot_table'] = """

~/scipy/pandas/pandas/core/reshape/reshape.py in pivot(self, index, columns, values)
406         else:
407             indexed = self._constructor_sliced(self[values].values,
--> 408                                                index=index)
409     return indexed.unstack(columns)
410

~/scipy/pandas/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
260                             'Length of passed values is {val}, '
261                             'index implies {ind}'
--> 262                             .format(val=len(data), ind=len(index)))
263                 except TypeError:
264                     pass

ValueError: Length of passed values is 8, index implies 2

# Fixed code
def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame":
    if columns is None:
        raise TypeError("pivot() missing 1 required argument: 'columns'")
    columns = columns if is_list_like(columns) else [columns]

    if values is None:
        cols: List[str] = []
        if index is None:
            pass
        elif is_list_like(index):
            cols = list(index)
        else:
            cols = [index]
        cols.extend(columns)

        append = index is None
        indexed = data.set_index(cols, append=append)
    else:
        if index is None:
            index = [Series(data.index, name=data.index.name)]
        elif is_list_like(index):
            index = [data[idx] for idx in index]
        else:
            index = [data[index]]

        data_columns = [data[col] for col in columns]
        index.extend(data_columns)
        index = MultiIndex.from_arrays(index)

        if is_list_like(values) and not isinstance(values, tuple):
            # Exclude tuple because it is seen as a single column name
            indexed = data._constructor(
                data[values].values, index=index, columns=values
            )
        else:
            indexed = data._constructor_sliced(data[values].values, index=index)
    return indexed.unstack(columns)