33
33
validate_numeric_casting ,
34
34
)
35
35
from pandas .core .dtypes .common import (
36
- _is_unorderable_exception ,
37
36
ensure_platform_int ,
38
37
is_bool ,
39
38
is_categorical_dtype ,
@@ -1015,26 +1014,24 @@ def __setitem__(self, key, value):
1015
1014
except (KeyError , ValueError ):
1016
1015
values = self ._values
1017
1016
if is_integer (key ) and not self .index .inferred_type == "integer" :
1017
+ # positional setter
1018
1018
values [key ] = value
1019
1019
else :
1020
+ # GH#12862 adding an new key to the Series
1020
1021
self .loc [key ] = value
1021
1022
1022
1023
except TypeError as e :
1023
1024
if isinstance (key , tuple ) and not isinstance (self .index , MultiIndex ):
1024
1025
raise ValueError ("Can only tuple-index with a MultiIndex" ) from e
1025
1026
1026
- # python 3 type errors should be raised
1027
- if _is_unorderable_exception (e ):
1028
- raise IndexError (key ) from e
1029
-
1030
1027
if com .is_bool_indexer (key ):
1031
1028
key = check_bool_indexer (self .index , key )
1032
1029
key = np .asarray (key , dtype = bool )
1033
1030
try :
1034
1031
self ._where (~ key , value , inplace = True )
1035
- return
1036
1032
except InvalidIndexError :
1037
1033
self ._set_values (key .astype (np .bool_ ), value )
1034
+ return
1038
1035
1039
1036
else :
1040
1037
self ._set_with (key , value )
@@ -1054,20 +1051,8 @@ def _set_with(self, key, value):
1054
1051
indexer = self .index ._convert_slice_indexer (key , kind = "getitem" )
1055
1052
return self ._set_values (indexer , value )
1056
1053
1057
- elif is_scalar (key ) and not is_integer (key ) and key not in self .index :
1058
- # GH#12862 adding an new key to the Series
1059
- # Note: have to exclude integers because that is ambiguously
1060
- # position-based
1061
- self .loc [key ] = value
1062
- return
1063
-
1064
1054
else :
1065
- if isinstance (key , tuple ):
1066
- try :
1067
- # TODO: no test cases that get here
1068
- self ._set_values (key , value )
1069
- except Exception :
1070
- pass
1055
+ assert not isinstance (key , tuple )
1071
1056
1072
1057
if is_scalar (key ):
1073
1058
key = [key ]
@@ -1084,7 +1069,7 @@ def _set_with(self, key, value):
1084
1069
if self .index .inferred_type == "integer" :
1085
1070
self ._set_labels (key , value )
1086
1071
else :
1087
- return self ._set_values (key , value )
1072
+ self ._set_values (key , value )
1088
1073
else :
1089
1074
self ._set_labels (key , value )
1090
1075
0 commit comments