@@ -228,76 +228,53 @@ def _assert_setitem_index_conversion(self, original_series, loc_key,
228
228
# check dtype explicitly for sure
229
229
assert temp .index .dtype == expected_dtype
230
230
231
- def test_setitem_index_object (self ):
231
+ @pytest .mark .parametrize ("val,exp_dtype" , [
232
+ ('x' , np .object ),
233
+ (5 , IndexError ),
234
+ (1.1 , np .object )])
235
+ def test_setitem_index_object (self , val , exp_dtype ):
232
236
obj = pd .Series ([1 , 2 , 3 , 4 ], index = list ('abcd' ))
233
237
assert obj .index .dtype == np .object
234
238
235
- # object + object -> object
236
- exp_index = pd .Index (list ('abcdx' ))
237
- self ._assert_setitem_index_conversion (obj , 'x' , exp_index , np .object )
238
-
239
- # object + int -> IndexError, regarded as location
240
- temp = obj .copy ()
241
- with pytest .raises (IndexError ):
242
- temp [5 ] = 5
243
-
244
- # object + float -> object
245
- exp_index = pd .Index (['a' , 'b' , 'c' , 'd' , 1.1 ])
246
- self ._assert_setitem_index_conversion (obj , 1.1 , exp_index , np .object )
239
+ if exp_dtype is IndexError :
240
+ # object + int -> IndexError, regarded as location
241
+ temp = obj .copy ()
242
+ with pytest .raises (exp_dtype ):
243
+ temp [5 ] = 5
244
+ else :
245
+ exp_index = pd .Index (list ('abcd' ) + [val ])
246
+ self ._assert_setitem_index_conversion (obj , val , exp_index , exp_dtype )
247
247
248
- def test_setitem_index_int64 (self ):
248
+ @pytest .mark .parametrize ("val,exp_dtype" , [
249
+ (5 , np .int64 ),
250
+ (1.1 , np .float64 ),
251
+ ('x' , np .object )])
252
+ def test_setitem_index_int64 (self , val , exp_dtype ):
249
253
# tests setitem with non-existing numeric key
250
254
obj = pd .Series ([1 , 2 , 3 , 4 ])
251
255
assert obj .index .dtype == np .int64
252
256
253
- # int + int -> int
254
- exp_index = pd .Index ([0 , 1 , 2 , 3 , 5 ])
255
- self ._assert_setitem_index_conversion (obj , 5 , exp_index , np .int64 )
257
+ exp_index = pd .Index ([0 , 1 , 2 , 3 , val ])
258
+ self ._assert_setitem_index_conversion (obj , val , exp_index , exp_dtype )
256
259
257
- # int + float -> float
258
- exp_index = pd .Index ([0 , 1 , 2 , 3 , 1.1 ])
259
- self ._assert_setitem_index_conversion (obj , 1.1 , exp_index , np .float64 )
260
-
261
- # int + object -> object
262
- exp_index = pd .Index ([0 , 1 , 2 , 3 , 'x' ])
263
- self ._assert_setitem_index_conversion (obj , 'x' , exp_index , np .object )
264
-
265
- def test_setitem_index_float64 (self ):
260
+ @pytest .mark .parametrize ("val,exp_dtype" , [
261
+ (5 , IndexError ),
262
+ (5.1 , np .float64 ),
263
+ ('x' , np .object )])
264
+ def test_setitem_index_float64 (self , val , exp_dtype ):
266
265
# tests setitem with non-existing numeric key
267
266
obj = pd .Series ([1 , 2 , 3 , 4 ], index = [1.1 , 2.1 , 3.1 , 4.1 ])
268
267
assert obj .index .dtype == np .float64
269
268
270
- # float + int -> int
271
- temp = obj .copy ()
272
- # TODO_GH12747 The result must be float
273
- with pytest .raises (IndexError ):
274
- temp [5 ] = 5
275
-
276
- # float + float -> float
277
- exp_index = pd .Index ([1.1 , 2.1 , 3.1 , 4.1 , 5.1 ])
278
- self ._assert_setitem_index_conversion (obj , 5.1 , exp_index , np .float64 )
269
+ if exp_dtype is IndexError :
270
+ # float + int -> int
271
+ temp = obj .copy ()
272
+ with pytest .raises (exp_dtype ):
273
+ temp [5 ] = 5
274
+ pytest .xfail ("TODO_GH12747 The result must be float" )
279
275
280
- # float + object -> object
281
- exp_index = pd .Index ([1.1 , 2.1 , 3.1 , 4.1 , 'x' ])
282
- self ._assert_setitem_index_conversion (obj , 'x' , exp_index , np .object )
283
-
284
- def test_setitem_index_complex128 (self ):
285
- pass
286
-
287
- def test_setitem_index_bool (self ):
288
- pass
289
-
290
- def test_setitem_index_datetime64 (self ):
291
- pass
292
-
293
- def test_setitem_index_datetime64tz (self ):
294
- pass
295
-
296
- def test_setitem_index_timedelta64 (self ):
297
- pass
298
-
299
- def test_setitem_index_period (self ):
300
- pass
276
+ exp_index = pd .Index ([1.1 , 2.1 , 3.1 , 4.1 , val ])
277
+ self ._assert_setitem_index_conversion (obj , val , exp_index , exp_dtype )
301
278
302
279
303
280
class TestInsertIndexCoercion (CoercionBase ):
0 commit comments