@@ -1131,8 +1131,7 @@ def __getitem__(self, key):
1131
1131
def _get_with (self , key ):
1132
1132
# other: fancy integer or otherwise
1133
1133
if isinstance (key , slice ):
1134
- indexer = self .index ._convert_slice_indexer (key , kind = "getitem" )
1135
- return self ._get_values (indexer )
1134
+ return self ._slice (key )
1136
1135
elif isinstance (key , ABCDataFrame ):
1137
1136
raise TypeError (
1138
1137
"Indexing a Series with DataFrame is not "
@@ -1148,7 +1147,6 @@ def _get_with(self, key):
1148
1147
return self ._get_values (key )
1149
1148
raise
1150
1149
1151
- # pragma: no cover
1152
1150
if not isinstance (key , (list , np .ndarray , Series , Index )):
1153
1151
key = list (key )
1154
1152
@@ -1165,19 +1163,18 @@ def _get_with(self, key):
1165
1163
elif key_type == "boolean" :
1166
1164
return self ._get_values (key )
1167
1165
1168
- try :
1169
- # handle the dup indexing case (GH 4246)
1170
- if isinstance (key , (list , tuple )):
1171
- return self .loc [key ]
1172
-
1173
- return self .reindex (key )
1174
- except Exception :
1175
- # [slice(0, 5, None)] will break if you convert to ndarray,
1176
- # e.g. as requested by np.median
1177
- # hack
1178
- if isinstance (key [0 ], slice ):
1166
+ if isinstance (key , (list , tuple )):
1167
+ # TODO: de-dup with tuple case handled above?
1168
+ # handle the dup indexing case GH#4246
1169
+ if len (key ) == 1 and isinstance (key [0 ], slice ):
1170
+ # [slice(0, 5, None)] will break if you convert to ndarray,
1171
+ # e.g. as requested by np.median
1172
+ # FIXME: hack
1179
1173
return self ._get_values (key )
1180
- raise
1174
+
1175
+ return self .loc [key ]
1176
+
1177
+ return self .reindex (key )
1181
1178
1182
1179
def _get_values_tuple (self , key ):
1183
1180
# mpl hackaround
@@ -1220,33 +1217,28 @@ def _get_value(self, label, takeable: bool = False):
1220
1217
1221
1218
def __setitem__ (self , key , value ):
1222
1219
key = com .apply_if_callable (key , self )
1220
+ cacher_needs_updating = self ._check_is_chained_assignment_possible ()
1223
1221
1224
- def setitem (key , value ):
1225
- try :
1226
- self ._set_with_engine (key , value )
1227
- return
1228
- except com .SettingWithCopyError :
1229
- raise
1230
- except (KeyError , ValueError ):
1231
- values = self ._values
1232
- if is_integer (key ) and not self .index .inferred_type == "integer" :
1233
-
1234
- values [key ] = value
1235
- return
1236
- elif key is Ellipsis :
1237
- self [:] = value
1238
- return
1239
-
1222
+ try :
1223
+ self ._set_with_engine (key , value )
1224
+ except com .SettingWithCopyError :
1225
+ raise
1226
+ except (KeyError , ValueError ):
1227
+ values = self ._values
1228
+ if is_integer (key ) and not self .index .inferred_type == "integer" :
1229
+ values [key ] = value
1230
+ elif key is Ellipsis :
1231
+ self [:] = value
1232
+ else :
1240
1233
self .loc [key ] = value
1241
- return
1242
1234
1243
- except TypeError as e :
1244
- if isinstance (key , tuple ) and not isinstance (self .index , MultiIndex ):
1245
- raise ValueError ("Can only tuple-index with a MultiIndex" )
1235
+ except TypeError as e :
1236
+ if isinstance (key , tuple ) and not isinstance (self .index , MultiIndex ):
1237
+ raise ValueError ("Can only tuple-index with a MultiIndex" )
1246
1238
1247
- # python 3 type errors should be raised
1248
- if _is_unorderable_exception (e ):
1249
- raise IndexError (key )
1239
+ # python 3 type errors should be raised
1240
+ if _is_unorderable_exception (e ):
1241
+ raise IndexError (key )
1250
1242
1251
1243
if com .is_bool_indexer (key ):
1252
1244
key = check_bool_indexer (self .index , key )
@@ -1258,9 +1250,6 @@ def setitem(key, value):
1258
1250
1259
1251
self ._set_with (key , value )
1260
1252
1261
- # do the setitem
1262
- cacher_needs_updating = self ._check_is_chained_assignment_possible ()
1263
- setitem (key , value )
1264
1253
if cacher_needs_updating :
1265
1254
self ._maybe_update_cacher ()
1266
1255
@@ -1282,20 +1271,21 @@ def _set_with(self, key, value):
1282
1271
if isinstance (key , slice ):
1283
1272
indexer = self .index ._convert_slice_indexer (key , kind = "getitem" )
1284
1273
return self ._set_values (indexer , value )
1274
+
1275
+ elif is_scalar (key ) and not is_integer (key ) and key not in self .index :
1276
+ # GH#12862 adding an new key to the Series
1277
+ # Note: have to exclude integers because that is ambiguously
1278
+ # position-based
1279
+ self .loc [key ] = value
1280
+ return
1281
+
1285
1282
else :
1286
1283
if isinstance (key , tuple ):
1287
1284
try :
1288
1285
self ._set_values (key , value )
1289
1286
except Exception :
1290
1287
pass
1291
1288
1292
- if is_scalar (key ) and not is_integer (key ) and key not in self .index :
1293
- # GH#12862 adding an new key to the Series
1294
- # Note: have to exclude integers because that is ambiguously
1295
- # position-based
1296
- self .loc [key ] = value
1297
- return
1298
-
1299
1289
if is_scalar (key ):
1300
1290
key = [key ]
1301
1291
elif not isinstance (key , (list , Series , np .ndarray )):
@@ -1306,6 +1296,7 @@ def _set_with(self, key, value):
1306
1296
1307
1297
if isinstance (key , Index ):
1308
1298
key_type = key .inferred_type
1299
+ key = key ._values
1309
1300
else :
1310
1301
key_type = lib .infer_dtype (key , skipna = False )
1311
1302
@@ -1320,10 +1311,7 @@ def _set_with(self, key, value):
1320
1311
self ._set_labels (key , value )
1321
1312
1322
1313
def _set_labels (self , key , value ):
1323
- if isinstance (key , Index ):
1324
- key = key .values
1325
- else :
1326
- key = com .asarray_tuplesafe (key )
1314
+ key = com .asarray_tuplesafe (key )
1327
1315
indexer = self .index .get_indexer (key )
1328
1316
mask = indexer == - 1
1329
1317
if mask .any ():
0 commit comments