@@ -1072,7 +1072,7 @@ def __setitem__(self, key, value) -> None:
1072
1072
# GH#12862 adding a new key to the Series
1073
1073
self .loc [key ] = value
1074
1074
1075
- except TypeError as err :
1075
+ except ( InvalidIndexError , TypeError ) as err :
1076
1076
if isinstance (key , tuple ) and not isinstance (self .index , MultiIndex ):
1077
1077
raise KeyError (
1078
1078
"key of type tuple not found and not a MultiIndex"
@@ -1094,8 +1094,7 @@ def __setitem__(self, key, value) -> None:
1094
1094
self ._maybe_update_cacher ()
1095
1095
1096
1096
def _set_with_engine (self , key , value ) -> None :
1097
- # fails with AttributeError for IntervalIndex
1098
- loc = self .index ._engine .get_loc (key )
1097
+ loc = self .index .get_loc (key )
1099
1098
# error: Argument 1 to "validate_numeric_casting" has incompatible type
1100
1099
# "Union[dtype, ExtensionDtype]"; expected "dtype"
1101
1100
validate_numeric_casting (self .dtype , value ) # type: ignore[arg-type]
@@ -1112,6 +1111,9 @@ def _set_with(self, key, value):
1112
1111
1113
1112
if is_scalar (key ):
1114
1113
key = [key ]
1114
+ elif is_iterator (key ):
1115
+ # Without this, the call to infer_dtype will consume the generator
1116
+ key = list (key )
1115
1117
1116
1118
if isinstance (key , Index ):
1117
1119
key_type = key .inferred_type
0 commit comments