@@ -3493,6 +3493,189 @@ def test_construction_with_alt(self):
3493
3493
def test_pickle_compat_construction (self ):
3494
3494
pass
3495
3495
3496
+ def test_construction_index_with_mixed_timezones (self ):
3497
+ # GH 11488
3498
+ # no tz results in DatetimeIndex
3499
+ result = Index ([Timestamp ('2011-01-01' ), Timestamp ('2011-01-02' )], name = 'idx' )
3500
+ exp = DatetimeIndex ([Timestamp ('2011-01-01' ), Timestamp ('2011-01-02' )], name = 'idx' )
3501
+ self .assert_index_equal (result , exp , exact = True )
3502
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3503
+ self .assertIsNone (result .tz )
3504
+
3505
+ # same tz results in DatetimeIndex
3506
+ result = Index ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3507
+ Timestamp ('2011-01-02 10:00' , tz = 'Asia/Tokyo' )], name = 'idx' )
3508
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-01-02 10:00' )], tz = 'Asia/Tokyo' , name = 'idx' )
3509
+ self .assert_index_equal (result , exp , exact = True )
3510
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3511
+ self .assertIsNotNone (result .tz )
3512
+ self .assertEqual (result .tz , exp .tz )
3513
+
3514
+ # same tz results in DatetimeIndex (DST)
3515
+ result = Index ([Timestamp ('2011-01-01 10:00' , tz = 'US/Eastern' ),
3516
+ Timestamp ('2011-08-01 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3517
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-08-01 10:00' )],
3518
+ tz = 'US/Eastern' , name = 'idx' )
3519
+ self .assert_index_equal (result , exp , exact = True )
3520
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3521
+ self .assertIsNotNone (result .tz )
3522
+ self .assertEqual (result .tz , exp .tz )
3523
+
3524
+ # different tz results in Index(dtype=object)
3525
+ result = Index ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3526
+ exp = Index ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3527
+ dtype = 'object' , name = 'idx' )
3528
+ self .assert_index_equal (result , exp , exact = True )
3529
+ self .assertFalse (isinstance (result , DatetimeIndex ))
3530
+
3531
+ result = Index ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3532
+ Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3533
+ exp = Index ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3534
+ Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3535
+ dtype = 'object' , name = 'idx' )
3536
+ self .assert_index_equal (result , exp , exact = True )
3537
+ self .assertFalse (isinstance (result , DatetimeIndex ))
3538
+
3539
+ # passing tz results in DatetimeIndex
3540
+ result = Index ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3541
+ tz = 'Asia/Tokyo' , name = 'idx' )
3542
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 19:00' ), Timestamp ('2011-01-03 00:00' )],
3543
+ tz = 'Asia/Tokyo' , name = 'idx' )
3544
+ self .assert_index_equal (result , exp , exact = True )
3545
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3546
+
3547
+ # length = 1
3548
+ result = Index ([Timestamp ('2011-01-01' )], name = 'idx' )
3549
+ exp = DatetimeIndex ([Timestamp ('2011-01-01' )], name = 'idx' )
3550
+ self .assert_index_equal (result , exp , exact = True )
3551
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3552
+ self .assertIsNone (result .tz )
3553
+
3554
+ # length = 1 with tz
3555
+ result = Index ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' )], name = 'idx' )
3556
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 10:00' )], tz = 'Asia/Tokyo' , name = 'idx' )
3557
+ self .assert_index_equal (result , exp , exact = True )
3558
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3559
+ self .assertIsNotNone (result .tz )
3560
+ self .assertEqual (result .tz , exp .tz )
3561
+
3562
+ def test_construction_index_with_mixed_timezones_with_NaT (self ):
3563
+ # GH 11488
3564
+ result = Index ([pd .NaT , Timestamp ('2011-01-01' ),
3565
+ pd .NaT , Timestamp ('2011-01-02' )], name = 'idx' )
3566
+ exp = DatetimeIndex ([pd .NaT , Timestamp ('2011-01-01' ),
3567
+ pd .NaT , Timestamp ('2011-01-02' )], name = 'idx' )
3568
+ self .assert_index_equal (result , exp , exact = True )
3569
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3570
+ self .assertIsNone (result .tz )
3571
+
3572
+ # same tz results in DatetimeIndex
3573
+ result = Index ([pd .NaT , Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3574
+ pd .NaT , Timestamp ('2011-01-02 10:00' , tz = 'Asia/Tokyo' )], name = 'idx' )
3575
+ exp = DatetimeIndex ([pd .NaT , Timestamp ('2011-01-01 10:00' ),
3576
+ pd .NaT , Timestamp ('2011-01-02 10:00' )], tz = 'Asia/Tokyo' , name = 'idx' )
3577
+ self .assert_index_equal (result , exp , exact = True )
3578
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3579
+ self .assertIsNotNone (result .tz )
3580
+ self .assertEqual (result .tz , exp .tz )
3581
+
3582
+ # same tz results in DatetimeIndex (DST)
3583
+ result = Index ([Timestamp ('2011-01-01 10:00' , tz = 'US/Eastern' ),
3584
+ pd .NaT , Timestamp ('2011-08-01 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3585
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 10:00' ), pd .NaT , Timestamp ('2011-08-01 10:00' )],
3586
+ tz = 'US/Eastern' , name = 'idx' )
3587
+ self .assert_index_equal (result , exp , exact = True )
3588
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3589
+ self .assertIsNotNone (result .tz )
3590
+ self .assertEqual (result .tz , exp .tz )
3591
+
3592
+ # different tz results in Index(dtype=object)
3593
+ result = Index ([pd .NaT , Timestamp ('2011-01-01 10:00' ),
3594
+ pd .NaT , Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3595
+ exp = Index ([pd .NaT , Timestamp ('2011-01-01 10:00' ),
3596
+ pd .NaT , Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3597
+ dtype = 'object' , name = 'idx' )
3598
+ self .assert_index_equal (result , exp , exact = True )
3599
+ self .assertFalse (isinstance (result , DatetimeIndex ))
3600
+
3601
+ result = Index ([pd .NaT , Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3602
+ pd .NaT , Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3603
+ exp = Index ([pd .NaT , Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3604
+ pd .NaT , Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3605
+ dtype = 'object' , name = 'idx' )
3606
+ self .assert_index_equal (result , exp , exact = True )
3607
+ self .assertFalse (isinstance (result , DatetimeIndex ))
3608
+
3609
+ # passing tz results in DatetimeIndex
3610
+ result = Index ([pd .NaT , Timestamp ('2011-01-01 10:00' ),
3611
+ pd .NaT , Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3612
+ tz = 'Asia/Tokyo' , name = 'idx' )
3613
+ exp = DatetimeIndex ([pd .NaT , Timestamp ('2011-01-01 19:00' ),
3614
+ pd .NaT , Timestamp ('2011-01-03 00:00' )],
3615
+ tz = 'Asia/Tokyo' , name = 'idx' )
3616
+ self .assert_index_equal (result , exp , exact = True )
3617
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3618
+
3619
+ # all NaT
3620
+ result = Index ([pd .NaT , pd .NaT ], name = 'idx' )
3621
+ exp = DatetimeIndex ([pd .NaT , pd .NaT ], name = 'idx' )
3622
+ self .assert_index_equal (result , exp , exact = True )
3623
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3624
+ self .assertIsNone (result .tz )
3625
+
3626
+ # all NaT with tz
3627
+ result = Index ([pd .NaT , pd .NaT ], tz = 'Asia/Tokyo' , name = 'idx' )
3628
+ exp = DatetimeIndex ([pd .NaT , pd .NaT ], tz = 'Asia/Tokyo' , name = 'idx' )
3629
+ self .assert_index_equal (result , exp , exact = True )
3630
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3631
+ self .assertIsNotNone (result .tz )
3632
+ self .assertEqual (result .tz , exp .tz )
3633
+
3634
+ def test_construction_dti_with_mixed_timezones (self ):
3635
+ # GH 11488 (not changed, added explicit tests)
3636
+
3637
+ # no tz results in DatetimeIndex
3638
+ result = DatetimeIndex ([Timestamp ('2011-01-01' ), Timestamp ('2011-01-02' )], name = 'idx' )
3639
+ exp = DatetimeIndex ([Timestamp ('2011-01-01' ), Timestamp ('2011-01-02' )], name = 'idx' )
3640
+ self .assert_index_equal (result , exp , exact = True )
3641
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3642
+
3643
+ # same tz results in DatetimeIndex
3644
+ result = DatetimeIndex ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3645
+ Timestamp ('2011-01-02 10:00' , tz = 'Asia/Tokyo' )], name = 'idx' )
3646
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-01-02 10:00' )], tz = 'Asia/Tokyo' , name = 'idx' )
3647
+ self .assert_index_equal (result , exp , exact = True )
3648
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3649
+
3650
+ # same tz results in DatetimeIndex (DST)
3651
+ result = DatetimeIndex ([Timestamp ('2011-01-01 10:00' , tz = 'US/Eastern' ),
3652
+ Timestamp ('2011-08-01 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3653
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-08-01 10:00' )],
3654
+ tz = 'US/Eastern' , name = 'idx' )
3655
+ self .assert_index_equal (result , exp , exact = True )
3656
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3657
+
3658
+ # different tz coerces tz-naive to tz-awareIndex(dtype=object)
3659
+ result = DatetimeIndex ([Timestamp ('2011-01-01 10:00' ),
3660
+ Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3661
+ exp = DatetimeIndex ([Timestamp ('2011-01-01 05:00' ), Timestamp ('2011-01-02 10:00' )],
3662
+ tz = 'US/Eastern' , name = 'idx' )
3663
+ self .assert_index_equal (result , exp , exact = True )
3664
+ self .assertTrue (isinstance (result , DatetimeIndex ))
3665
+
3666
+ # tz mismatch affecting to tz-aware raises TypeError/ValueError
3667
+ with tm .assertRaises (ValueError ):
3668
+ DatetimeIndex ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3669
+ Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], name = 'idx' )
3670
+
3671
+ with tm .assertRaises (TypeError ):
3672
+ DatetimeIndex ([Timestamp ('2011-01-01 10:00' ), Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )],
3673
+ tz = 'Asia/Tokyo' , name = 'idx' )
3674
+
3675
+ with tm .assertRaises (ValueError ):
3676
+ DatetimeIndex ([Timestamp ('2011-01-01 10:00' , tz = 'Asia/Tokyo' ),
3677
+ Timestamp ('2011-01-02 10:00' , tz = 'US/Eastern' )], tz = 'US/Eastern' , name = 'idx' )
3678
+
3496
3679
def test_get_loc (self ):
3497
3680
idx = pd .date_range ('2000-01-01' , periods = 3 )
3498
3681
0 commit comments