Skip to content

Commit 08f1742

Browse files
committed
Parametrized TestInsertIndexCoercion
1 parent a259b64 commit 08f1742

File tree

1 file changed

+38
-76
lines changed

1 file changed

+38
-76
lines changed

pandas/tests/indexing/test_coercion.py

+38-76
Original file line numberDiff line numberDiff line change
@@ -400,66 +400,42 @@ def _assert_insert_conversion(self, original, value,
400400
tm.assert_index_equal(res, expected)
401401
assert res.dtype == expected_dtype
402402

403-
def test_insert_index_object(self):
403+
@pytest.mark.parametrize("insert, coerced_val, coerced_dtype", [
404+
(1, 1, np.object),
405+
(1.1, 1.1, np.object),
406+
(False, False, np.object),
407+
('x', 'x', np.object)])
408+
def test_insert_index_object(self, insert, coerced_val, coerced_dtype):
404409
obj = pd.Index(list('abcd'))
405410
assert obj.dtype == np.object
406411

407-
# object + int -> object
408-
exp = pd.Index(['a', 1, 'b', 'c', 'd'])
409-
self._assert_insert_conversion(obj, 1, exp, np.object)
410-
411-
# object + float -> object
412-
exp = pd.Index(['a', 1.1, 'b', 'c', 'd'])
413-
self._assert_insert_conversion(obj, 1.1, exp, np.object)
414-
415-
# object + bool -> object
416-
res = obj.insert(1, False)
417-
tm.assert_index_equal(res, pd.Index(['a', False, 'b', 'c', 'd']))
418-
assert res.dtype == np.object
419-
420-
# object + object -> object
421-
exp = pd.Index(['a', 'x', 'b', 'c', 'd'])
422-
self._assert_insert_conversion(obj, 'x', exp, np.object)
412+
exp = pd.Index(['a', coerced_val, 'b', 'c', 'd'])
413+
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
423414

424-
def test_insert_index_int64(self):
415+
@pytest.mark.parametrize("insert, coerced_val, coerced_dtype", [
416+
(1, 1, np.int64),
417+
(1.1, 1.1, np.float64),
418+
(False, 0, int),
419+
('x', 'x', np.object)])
420+
def test_insert_index_int64(self, insert, coerced_val, coerced_dtype):
425421
obj = pd.Int64Index([1, 2, 3, 4])
426422
assert obj.dtype == np.int64
427423

428-
# int + int -> int
429-
exp = pd.Index([1, 1, 2, 3, 4])
430-
self._assert_insert_conversion(obj, 1, exp, np.int64)
424+
exp = pd.Index([1, coerced_val, 2, 3, 4])
425+
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
431426

432-
# int + float -> float
433-
exp = pd.Index([1, 1.1, 2, 3, 4])
434-
self._assert_insert_conversion(obj, 1.1, exp, np.float64)
435-
436-
# int + bool -> int
437-
exp = pd.Index([1, 0, 2, 3, 4])
438-
self._assert_insert_conversion(obj, False, exp, np.int64)
439-
440-
# int + object -> object
441-
exp = pd.Index([1, 'x', 2, 3, 4])
442-
self._assert_insert_conversion(obj, 'x', exp, np.object)
443-
444-
def test_insert_index_float64(self):
427+
@pytest.mark.parametrize("insert, coerced_val, coerced_dtype", [
428+
(1, 1., np.float64),
429+
(1.1, 1.1, np.float64),
430+
(False, 0., np.float64),
431+
('x', 'x', np.object)])
432+
def test_insert_index_float64(self, insert, coerced_val, coerced_dtype):
445433
obj = pd.Float64Index([1., 2., 3., 4.])
446434
assert obj.dtype == np.float64
447435

448436
# float + int -> int
449-
exp = pd.Index([1., 1., 2., 3., 4.])
450-
self._assert_insert_conversion(obj, 1, exp, np.float64)
451-
452-
# float + float -> float
453-
exp = pd.Index([1., 1.1, 2., 3., 4.])
454-
self._assert_insert_conversion(obj, 1.1, exp, np.float64)
455-
456-
# float + bool -> float
457-
exp = pd.Index([1., 0., 2., 3., 4.])
458-
self._assert_insert_conversion(obj, False, exp, np.float64)
459-
460-
# float + object -> object
461-
exp = pd.Index([1., 'x', 2., 3., 4.])
462-
self._assert_insert_conversion(obj, 'x', exp, np.object)
437+
exp = pd.Index([1., coerced_val, 2., 3., 4.])
438+
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
463439

464440
def test_insert_index_complex128(self):
465441
pass
@@ -534,41 +510,27 @@ def test_insert_index_timedelta64(self):
534510
with tm.assert_raises_regex(TypeError, msg):
535511
obj.insert(1, 1)
536512

537-
def test_insert_index_period(self):
513+
@pytest.mark.parametrize("insert, coerced_val, coerced_dtype", [
514+
(pd.Period('2012-01', freq='M'), '2012-01', 'period[M]'),
515+
(pd.Timestamp('2012-01-01'), pd.Timestamp('2012-01-01'), np.object),
516+
(1, 1, np.object),
517+
('x', 'x', np.object)])
518+
def test_insert_index_period(self, insert, coerced_val, coerced_dtype):
538519
obj = pd.PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04'],
539520
freq='M')
540521
assert obj.dtype == 'period[M]'
541522

542-
# period + period => period
543-
exp = pd.PeriodIndex(['2011-01', '2012-01', '2011-02',
544-
'2011-03', '2011-04'], freq='M')
545-
self._assert_insert_conversion(obj, pd.Period('2012-01', freq='M'),
546-
exp, 'period[M]')
547-
548-
# period + datetime64 => object
549-
exp = pd.Index([pd.Period('2011-01', freq='M'),
550-
pd.Timestamp('2012-01-01'),
551-
pd.Period('2011-02', freq='M'),
552-
pd.Period('2011-03', freq='M'),
553-
pd.Period('2011-04', freq='M')], freq='M')
554-
self._assert_insert_conversion(obj, pd.Timestamp('2012-01-01'),
555-
exp, np.object)
556-
557-
# period + int => object
558-
exp = pd.Index([pd.Period('2011-01', freq='M'),
559-
1,
560-
pd.Period('2011-02', freq='M'),
561-
pd.Period('2011-03', freq='M'),
562-
pd.Period('2011-04', freq='M')], freq='M')
563-
self._assert_insert_conversion(obj, 1, exp, np.object)
523+
if isinstance(insert, pd.Period):
524+
index_type = pd.PeriodIndex
525+
else:
526+
index_type = pd.Index
564527

565-
# period + object => object
566-
exp = pd.Index([pd.Period('2011-01', freq='M'),
567-
'x',
528+
exp = index_type([pd.Period('2011-01', freq='M'),
529+
coerced_val,
568530
pd.Period('2011-02', freq='M'),
569531
pd.Period('2011-03', freq='M'),
570-
pd.Period('2011-04', freq='M')], freq='M')
571-
self._assert_insert_conversion(obj, 'x', exp, np.object)
532+
pd.Period('2011-04', freq='M')], freq='M')
533+
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
572534

573535

574536
class TestWhereCoercion(CoercionBase):

0 commit comments

Comments
 (0)