@@ -572,6 +572,40 @@ def test_minmax(self):
572
572
obj = DatetimeIndex ([pd .NaT , pd .NaT , pd .NaT ])
573
573
self .assertTrue (pd .isnull (getattr (obj , op )()))
574
574
575
+ def test_representation (self ):
576
+ idx1 = DatetimeIndex ([], freq = 'D' )
577
+ idx2 = DatetimeIndex (['2011-01-01' ], freq = 'D' )
578
+ idx3 = DatetimeIndex (['2011-01-01' , '2011-01-02' ], freq = 'D' )
579
+ idx4 = DatetimeIndex (['2011-01-01' , '2011-01-02' , '2011-01-03' ], freq = 'D' )
580
+ idx5 = DatetimeIndex (['2011-01-01 09:00' , '2011-01-01 10:00' , '2011-01-01 11:00' ],
581
+ freq = 'H' , tz = 'Asia/Tokyo' )
582
+ idx6 = DatetimeIndex (['2011-01-01 09:00' , '2011-01-01 10:00' , pd .NaT ],
583
+ tz = 'US/Eastern' )
584
+
585
+ exp1 = """<class 'pandas.tseries.index.DatetimeIndex'>
586
+ Length: 0, Freq: D, Timezone: None"""
587
+ exp2 = """<class 'pandas.tseries.index.DatetimeIndex'>
588
+ [2011-01-01]
589
+ Length: 1, Freq: D, Timezone: None"""
590
+ exp3 = """<class 'pandas.tseries.index.DatetimeIndex'>
591
+ [2011-01-01, 2011-01-02]
592
+ Length: 2, Freq: D, Timezone: None"""
593
+ exp4 = """<class 'pandas.tseries.index.DatetimeIndex'>
594
+ [2011-01-01, ..., 2011-01-03]
595
+ Length: 3, Freq: D, Timezone: None"""
596
+ exp5 = """<class 'pandas.tseries.index.DatetimeIndex'>
597
+ [2011-01-01 09:00:00+09:00, ..., 2011-01-01 11:00:00+09:00]
598
+ Length: 3, Freq: H, Timezone: Asia/Tokyo"""
599
+ exp6 = """<class 'pandas.tseries.index.DatetimeIndex'>
600
+ [2011-01-01 09:00:00-05:00, ..., NaT]
601
+ Length: 3, Freq: None, Timezone: US/Eastern"""
602
+
603
+ for idx , expected in zip ([idx1 , idx2 , idx3 , idx4 , idx5 , idx6 ],
604
+ [exp1 , exp2 , exp3 , exp4 , exp5 , exp6 ]):
605
+ for func in ['__repr__' , '__unicode__' , '__str__' ]:
606
+ result = getattr (idx , func )()
607
+ self .assertEqual (result , expected )
608
+
575
609
576
610
class TestPeriodIndexOps (Ops ):
577
611
_allowed = '_allow_period_index_ops'
@@ -650,6 +684,52 @@ def test_minmax(self):
650
684
self .assertEqual (result .ordinal , tslib .iNaT )
651
685
self .assertEqual (result .freq , 'M' )
652
686
687
+ def test_representation (self ):
688
+ # GH 7601
689
+ idx1 = PeriodIndex ([], freq = 'D' )
690
+ idx2 = PeriodIndex (['2011-01-01' ], freq = 'D' )
691
+ idx3 = PeriodIndex (['2011-01-01' , '2011-01-02' ], freq = 'D' )
692
+ idx4 = PeriodIndex (['2011-01-01' , '2011-01-02' , '2011-01-03' ], freq = 'D' )
693
+ idx5 = PeriodIndex (['2011' , '2012' , '2013' ], freq = 'A' )
694
+ idx6 = PeriodIndex (['2011-01-01 09:00' , '2012-02-01 10:00' , 'NaT' ], freq = 'H' )
695
+
696
+ idx7 = pd .period_range ('2013Q1' , periods = 1 , freq = "Q" )
697
+ idx8 = pd .period_range ('2013Q1' , periods = 2 , freq = "Q" )
698
+ idx9 = pd .period_range ('2013Q1' , periods = 3 , freq = "Q" )
699
+
700
+ exp1 = """<class 'pandas.tseries.period.PeriodIndex'>
701
+ Length: 0, Freq: D"""
702
+ exp2 = """<class 'pandas.tseries.period.PeriodIndex'>
703
+ [2011-01-01]
704
+ Length: 1, Freq: D"""
705
+ exp3 = """<class 'pandas.tseries.period.PeriodIndex'>
706
+ [2011-01-01, 2011-01-02]
707
+ Length: 2, Freq: D"""
708
+ exp4 = """<class 'pandas.tseries.period.PeriodIndex'>
709
+ [2011-01-01, ..., 2011-01-03]
710
+ Length: 3, Freq: D"""
711
+ exp5 = """<class 'pandas.tseries.period.PeriodIndex'>
712
+ [2011, ..., 2013]
713
+ Length: 3, Freq: A-DEC"""
714
+ exp6 = """<class 'pandas.tseries.period.PeriodIndex'>
715
+ [2011-01-01 09:00, ..., NaT]
716
+ Length: 3, Freq: H"""
717
+ exp7 = """<class 'pandas.tseries.period.PeriodIndex'>
718
+ [2013Q1]
719
+ Length: 1, Freq: Q-DEC"""
720
+ exp8 = """<class 'pandas.tseries.period.PeriodIndex'>
721
+ [2013Q1, 2013Q2]
722
+ Length: 2, Freq: Q-DEC"""
723
+ exp9 = """<class 'pandas.tseries.period.PeriodIndex'>
724
+ [2013Q1, ..., 2013Q3]
725
+ Length: 3, Freq: Q-DEC"""
726
+
727
+ for idx , expected in zip ([idx1 , idx2 , idx3 , idx4 , idx5 , idx6 , idx7 , idx8 , idx9 ],
728
+ [exp1 , exp2 , exp3 , exp4 , exp5 , exp6 , exp7 , exp8 , exp9 ]):
729
+ for func in ['__repr__' , '__unicode__' , '__str__' ]:
730
+ result = getattr (idx , func )()
731
+ self .assertEqual (result , expected )
732
+
653
733
654
734
if __name__ == '__main__' :
655
735
import nose
0 commit comments