API: better error-handling for df.set_index by h-vetinari · Pull Request #22486 · pandas-dev/pandas (original) (raw)
this is not true. either all have to be scalars, or all list-likes (yes they don't have to be the same, just list-convertible).
This is wrong on both counts. On master, a mix of column labels and a list:
>>> df = pd.DataFrame({'a': [1, 2], 'b': [11, 12]})
>>> df
a b
0 1 11
1 2 12
>>> df.set_index(['a', [101, 102]])
b
a
1 101 11
2 102 12
Regarding the argument types, the list I wrote (and test) is exhaustive (see https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py#L3908) - currently only Series/Index/MultiIndex/list/ndarray
are allowed, everything else gets tried as a frame key. This is one of the points in #22484.
>>> from pandas.core.dtypes.common import is_list_like
>>> LL = iter([21, 22])
>>> is_list_like(LL)
True
>>> df.set_index(LL)
KeyError: "None of [Int64Index([21, 22], dtype='int64')] are in the [columns]"