@@ -3888,25 +3888,25 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
3888
3888
3 2013 7 84
3889
3889
4 2014 10 31
3890
3890
"""
3891
- from pandas import Series
3892
-
3893
3891
if not isinstance (keys , list ):
3894
3892
keys = [keys ]
3895
3893
3896
- # collect elements from "keys" that are not allowed array types
3897
- col_labels = [x for x in keys
3898
- if not isinstance (x , (Series , Index , MultiIndex ,
3899
- list , np .ndarray ))]
3900
- if any (x not in self for x in col_labels ):
3901
- # if there are any labels that are invalid, we raise a KeyError
3902
- missing = [x for x in col_labels if x not in self ]
3894
+ missing = []
3895
+ for x in keys :
3896
+ if not (is_scalar (x ) or isinstance (x , tuple )):
3897
+ if not isinstance (x , (ABCSeries , ABCIndexClass , ABCMultiIndex ,
3898
+ list , np .ndarray )):
3899
+ raise TypeError ('keys may only contain a combination of '
3900
+ 'the following: valid column keys, '
3901
+ 'Series, Index, MultiIndex, list or '
3902
+ 'np.ndarray' )
3903
+ else :
3904
+ if x not in self :
3905
+ missing .append (x )
3906
+
3907
+ if missing :
3903
3908
raise KeyError ('{}' .format (missing ))
3904
- elif len (set (col_labels )) < len (col_labels ):
3905
- # if all are valid labels, but there are duplicates
3906
- dup = Series (col_labels )
3907
- dup = list (dup .loc [dup .duplicated ()])
3908
- raise ValueError ('Passed duplicate column names '
3909
- 'to keys: {dup}' .format (dup = dup ))
3909
+
3910
3910
vi = verify_integrity
3911
3911
return super (DataFrame , self ).set_index (keys = keys , drop = drop ,
3912
3912
append = append , inplace = inplace ,
0 commit comments