|
60 | 60 | is_sequence, |
61 | 61 | is_named_tuple) |
62 | 62 | from pandas.core.dtypes.concat import _get_sliced_frame_result_type |
63 | | -from pandas.core.dtypes.generic import (ABCSeries, ABCIndexClass, |
64 | | - ABCMultiIndex, ABCDataFrame) |
| 63 | +from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass, ABCMultiIndex |
65 | 64 | from pandas.core.dtypes.missing import isna, notna |
66 | 65 |
|
67 | 66 |
|
@@ -3955,16 +3954,17 @@ def set_index(self, keys, drop=True, append=False, inplace=False, |
3955 | 3954 | keys = [keys] |
3956 | 3955 |
|
3957 | 3956 | missing = [] |
3958 | | - for x in keys: |
3959 | | - if (is_scalar(x) or isinstance(x, tuple)) and x in self: |
| 3957 | + for col in keys: |
| 3958 | + if (is_scalar(col) or isinstance(col, tuple)) and col in self: |
| 3959 | + # tuples can be both column keys or list-likes |
| 3960 | + # if they are valid column keys, everything is fine |
3960 | 3961 | continue |
3961 | | - elif is_scalar(x) and x not in self: |
3962 | | - # a tuple gets tried as column key first; |
3963 | | - # otherwise considered as a list-like; i.e. not missing |
3964 | | - missing.append(x) |
3965 | | - elif (not is_list_like(x) or isinstance(x, set) |
3966 | | - or isinstance(x, ABCDataFrame) |
3967 | | - or (isinstance(x, np.ndarray) and x.ndim > 1)): |
| 3962 | + elif is_scalar(col) and col not in self: |
| 3963 | + # tuples that are not column keys are considered list-like, |
| 3964 | + # not considered missing |
| 3965 | + missing.append(col) |
| 3966 | + elif (not is_list_like(col) or isinstance(col, set) |
| 3967 | + or getattr(col, 'ndim', 1) > 1): |
3968 | 3968 | raise TypeError('The parameter "keys" may only contain a ' |
3969 | 3969 | 'combination of valid column keys and ' |
3970 | 3970 | 'one-dimensional list-likes') |
|
0 commit comments