@@ -3516,44 +3516,163 @@ def test_series_partial_set(self):
3516
3516
# Regression from GH4825
3517
3517
ser = Series ([0.1 , 0.2 ], index = [1 , 2 ])
3518
3518
3519
- # ToDo: check_index_type can be True after GH 11497
3520
-
3521
3519
# loc
3522
3520
expected = Series ([np .nan , 0.2 , np .nan ], index = [3 , 2 , 3 ])
3523
3521
result = ser .loc [[3 , 2 , 3 ]]
3524
- assert_series_equal (result , expected , check_index_type = False )
3522
+ assert_series_equal (result , expected , check_index_type = True )
3523
+
3524
+ expected = Series ([np .nan , 0.2 , np .nan , np .nan ], index = [3 , 2 , 3 , 'x' ])
3525
+ result = ser .loc [[3 , 2 , 3 , 'x' ]]
3526
+ assert_series_equal (result , expected , check_index_type = True )
3527
+
3528
+ expected = Series ([0.2 , 0.2 , 0.1 ], index = [2 , 2 , 1 ])
3529
+ result = ser .loc [[2 , 2 , 1 ]]
3530
+ assert_series_equal (result , expected , check_index_type = True )
3531
+
3532
+ expected = Series ([0.2 , 0.2 , np .nan , 0.1 ], index = [2 , 2 , 'x' , 1 ])
3533
+ result = ser .loc [[2 , 2 , 'x' , 1 ]]
3534
+ assert_series_equal (result , expected , check_index_type = True )
3525
3535
3526
3536
# raises as nothing in in the index
3527
3537
self .assertRaises (KeyError , lambda : ser .loc [[3 , 3 , 3 ]])
3528
3538
3529
3539
expected = Series ([0.2 , 0.2 , np .nan ], index = [2 , 2 , 3 ])
3530
3540
result = ser .loc [[2 , 2 , 3 ]]
3531
- assert_series_equal (result , expected , check_index_type = False )
3541
+ assert_series_equal (result , expected , check_index_type = True )
3532
3542
3533
3543
expected = Series ([0.3 , np .nan , np .nan ], index = [3 , 4 , 4 ])
3534
3544
result = Series ([0.1 , 0.2 , 0.3 ], index = [1 , 2 , 3 ]).loc [[3 , 4 , 4 ]]
3535
- assert_series_equal (result , expected , check_index_type = False )
3545
+ assert_series_equal (result , expected , check_index_type = True )
3536
3546
3537
3547
expected = Series ([np .nan , 0.3 , 0.3 ], index = [5 , 3 , 3 ])
3538
3548
result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = [1 , 2 , 3 , 4 ]).loc [[5 , 3 , 3 ]]
3539
- assert_series_equal (result , expected , check_index_type = False )
3549
+ assert_series_equal (result , expected , check_index_type = True )
3540
3550
3541
3551
expected = Series ([np .nan , 0.4 , 0.4 ], index = [5 , 4 , 4 ])
3542
3552
result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = [1 , 2 , 3 , 4 ]).loc [[5 , 4 , 4 ]]
3543
- assert_series_equal (result , expected , check_index_type = False )
3553
+ assert_series_equal (result , expected , check_index_type = True )
3544
3554
3545
3555
expected = Series ([0.4 , np .nan , np .nan ], index = [7 , 2 , 2 ])
3546
3556
result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = [4 , 5 , 6 , 7 ]).loc [[7 , 2 , 2 ]]
3547
- assert_series_equal (result , expected , check_index_type = False )
3557
+ assert_series_equal (result , expected , check_index_type = True )
3548
3558
3549
3559
expected = Series ([0.4 , np .nan , np .nan ], index = [4 , 5 , 5 ])
3550
3560
result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = [1 , 2 , 3 , 4 ]).loc [[4 , 5 , 5 ]]
3551
- assert_series_equal (result , expected , check_index_type = False )
3561
+ assert_series_equal (result , expected , check_index_type = True )
3552
3562
3553
3563
# iloc
3554
3564
expected = Series ([0.2 , 0.2 , 0.1 , 0.1 ], index = [2 , 2 , 1 , 1 ])
3555
3565
result = ser .iloc [[1 , 1 , 0 , 0 ]]
3556
- assert_series_equal (result , expected , check_index_type = False )
3566
+ assert_series_equal (result , expected , check_index_type = True )
3567
+
3568
+ def test_series_partial_set_with_name (self ):
3569
+ # GH 11497
3570
+
3571
+ idx = Index ([1 , 2 ], dtype = 'int64' , name = 'idx' )
3572
+ ser = Series ([0.1 , 0.2 ], index = idx , name = 's' )
3573
+
3574
+ # loc
3575
+ exp_idx = Index ([3 , 2 , 3 ], dtype = 'int64' , name = 'idx' )
3576
+ expected = Series ([np .nan , 0.2 , np .nan ], index = exp_idx , name = 's' )
3577
+ result = ser .loc [[3 , 2 , 3 ]]
3578
+ assert_series_equal (result , expected , check_index_type = True )
3579
+
3580
+ exp_idx = Index ([3 , 2 , 3 , 'x' ], dtype = 'object' , name = 'idx' )
3581
+ expected = Series ([np .nan , 0.2 , np .nan , np .nan ], index = exp_idx , name = 's' )
3582
+ result = ser .loc [[3 , 2 , 3 , 'x' ]]
3583
+ assert_series_equal (result , expected , check_index_type = True )
3584
+
3585
+ exp_idx = Index ([2 , 2 , 1 ], dtype = 'int64' , name = 'idx' )
3586
+ expected = Series ([0.2 , 0.2 , 0.1 ], index = exp_idx , name = 's' )
3587
+ result = ser .loc [[2 , 2 , 1 ]]
3588
+ assert_series_equal (result , expected , check_index_type = True )
3589
+
3590
+ exp_idx = Index ([2 , 2 , 'x' , 1 ], dtype = 'object' , name = 'idx' )
3591
+ expected = Series ([0.2 , 0.2 , np .nan , 0.1 ], index = exp_idx , name = 's' )
3592
+ result = ser .loc [[2 , 2 , 'x' , 1 ]]
3593
+ assert_series_equal (result , expected , check_index_type = True )
3594
+
3595
+ # raises as nothing in in the index
3596
+ self .assertRaises (KeyError , lambda : ser .loc [[3 , 3 , 3 ]])
3597
+
3598
+ exp_idx = Index ([2 , 2 , 3 ], dtype = 'int64' , name = 'idx' )
3599
+ expected = Series ([0.2 , 0.2 , np .nan ], index = exp_idx , name = 's' )
3600
+ result = ser .loc [[2 , 2 , 3 ]]
3601
+ assert_series_equal (result , expected , check_index_type = True )
3602
+
3603
+ exp_idx = Index ([3 , 4 , 4 ], dtype = 'int64' , name = 'idx' )
3604
+ expected = Series ([0.3 , np .nan , np .nan ], index = exp_idx , name = 's' )
3605
+ idx = Index ([1 , 2 , 3 ], dtype = 'int64' , name = 'idx' )
3606
+ result = Series ([0.1 , 0.2 , 0.3 ], index = idx , name = 's' ).loc [[3 , 4 , 4 ]]
3607
+ assert_series_equal (result , expected , check_index_type = True )
3608
+
3609
+ exp_idx = Index ([5 , 3 , 3 ], dtype = 'int64' , name = 'idx' )
3610
+ expected = Series ([np .nan , 0.3 , 0.3 ], index = exp_idx , name = 's' )
3611
+ idx = Index ([1 , 2 , 3 , 4 ], dtype = 'int64' , name = 'idx' )
3612
+ result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = idx , name = 's' ).loc [[5 , 3 , 3 ]]
3613
+ assert_series_equal (result , expected , check_index_type = True )
3614
+
3615
+ exp_idx = Index ([5 , 4 , 4 ], dtype = 'int64' , name = 'idx' )
3616
+ expected = Series ([np .nan , 0.4 , 0.4 ], index = exp_idx , name = 's' )
3617
+ idx = Index ([1 , 2 , 3 , 4 ], dtype = 'int64' , name = 'idx' )
3618
+ result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = idx , name = 's' ).loc [[5 , 4 , 4 ]]
3619
+ assert_series_equal (result , expected , check_index_type = True )
3620
+
3621
+ exp_idx = Index ([7 , 2 , 2 ], dtype = 'int64' , name = 'idx' )
3622
+ expected = Series ([0.4 , np .nan , np .nan ], index = exp_idx , name = 's' )
3623
+ idx = Index ([4 , 5 , 6 , 7 ], dtype = 'int64' , name = 'idx' )
3624
+ result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = idx , name = 's' ).loc [[7 , 2 , 2 ]]
3625
+ assert_series_equal (result , expected , check_index_type = True )
3626
+
3627
+ exp_idx = Index ([4 , 5 , 5 ], dtype = 'int64' , name = 'idx' )
3628
+ expected = Series ([0.4 , np .nan , np .nan ], index = exp_idx , name = 's' )
3629
+ idx = Index ([1 , 2 , 3 , 4 ], dtype = 'int64' , name = 'idx' )
3630
+ result = Series ([0.1 , 0.2 , 0.3 , 0.4 ], index = idx , name = 's' ).loc [[4 , 5 , 5 ]]
3631
+ assert_series_equal (result , expected , check_index_type = True )
3632
+
3633
+ # iloc
3634
+ exp_idx = Index ([2 , 2 , 1 , 1 ], dtype = 'int64' , name = 'idx' )
3635
+ expected = Series ([0.2 , 0.2 , 0.1 , 0.1 ], index = exp_idx , name = 's' )
3636
+ result = ser .iloc [[1 ,1 ,0 ,0 ]]
3637
+ assert_series_equal (result , expected , check_index_type = True )
3638
+
3639
+ def test_series_partial_set_datetime (self ):
3640
+ # GH 11497
3641
+
3642
+ idx = date_range ('2011-01-01' , '2011-01-02' , freq = 'D' , name = 'idx' )
3643
+ ser = Series ([0.1 , 0.2 ], index = idx , name = 's' )
3644
+
3645
+ result = ser .loc [[Timestamp ('2011-01-01' ), Timestamp ('2011-01-02' )]]
3646
+ exp = Series ([0.1 , 0.2 ], index = idx , name = 's' )
3647
+ assert_series_equal (result , exp , check_index_type = True )
3648
+
3649
+ keys = [Timestamp ('2011-01-02' ), Timestamp ('2011-01-02' ), Timestamp ('2011-01-01' )]
3650
+ exp = Series ([0.2 , 0.2 , 0.1 ], index = pd .DatetimeIndex (keys , name = 'idx' ), name = 's' )
3651
+ assert_series_equal (ser .loc [keys ], exp , check_index_type = True )
3652
+
3653
+ keys = [Timestamp ('2011-01-03' ), Timestamp ('2011-01-02' ), Timestamp ('2011-01-03' )]
3654
+ exp = Series ([np .nan , 0.2 , np .nan ], index = pd .DatetimeIndex (keys , name = 'idx' ), name = 's' )
3655
+ assert_series_equal (ser .loc [keys ], exp , check_index_type = True )
3656
+
3657
+ def test_series_partial_set_period (self ):
3658
+ # GH 11497
3659
+
3660
+ idx = pd .period_range ('2011-01-01' , '2011-01-02' , freq = 'D' , name = 'idx' )
3661
+ ser = Series ([0.1 , 0.2 ], index = idx , name = 's' )
3662
+
3663
+ result = ser .loc [[pd .Period ('2011-01-01' , freq = 'D' ), pd .Period ('2011-01-02' , freq = 'D' )]]
3664
+ exp = Series ([0.1 , 0.2 ], index = idx , name = 's' )
3665
+ assert_series_equal (result , exp , check_index_type = True )
3666
+
3667
+ keys = [pd .Period ('2011-01-02' , freq = 'D' ), pd .Period ('2011-01-02' , freq = 'D' ),
3668
+ pd .Period ('2011-01-01' , freq = 'D' )]
3669
+ exp = Series ([0.2 , 0.2 , 0.1 ], index = pd .PeriodIndex (keys , name = 'idx' ), name = 's' )
3670
+ assert_series_equal (ser .loc [keys ], exp , check_index_type = True )
3671
+
3672
+ keys = [pd .Period ('2011-01-03' , freq = 'D' ), pd .Period ('2011-01-02' , freq = 'D' ),
3673
+ pd .Period ('2011-01-03' , freq = 'D' )]
3674
+ exp = Series ([np .nan , 0.2 , np .nan ], index = pd .PeriodIndex (keys , name = 'idx' ), name = 's' )
3675
+ assert_series_equal (ser .loc [keys ], exp , check_index_type = True )
3557
3676
3558
3677
def test_partial_set_invalid (self ):
3559
3678
0 commit comments