@@ -553,11 +553,9 @@ class PeriodIndex(Int64Index):
553
553
__le__ = _period_index_cmp ('__le__' )
554
554
__ge__ = _period_index_cmp ('__ge__' )
555
555
556
- def __new__ (cls , data = None , ordinal = None ,
557
- freq = None , start = None , end = None , periods = None ,
558
- copy = False , name = None ,
559
- year = None , month = None , quarter = None , day = None ,
560
- hour = None , minute = None , second = None ,
556
+ def __new__ (cls , data = None , ordinal = None , freq = None , start = None , end = None ,
557
+ periods = None , copy = False , name = None , year = None , month = None ,
558
+ quarter = None , day = None , hour = None , minute = None , second = None ,
561
559
tz = None ):
562
560
563
561
freq = _freq_mod .get_standard_freq (freq )
@@ -649,19 +647,18 @@ def _from_arraylike(cls, data, freq, tz):
649
647
freq = getattr (data [0 ], 'freq' , None )
650
648
651
649
if freq is None :
652
- raise ValueError (( 'freq not specified and cannot be '
653
- 'inferred from first element' ) )
650
+ raise ValueError ('freq not specified and cannot be '
651
+ 'inferred from first element' )
654
652
655
- if np .issubdtype (data .dtype , np .datetime64 ):
656
- data = dt64arr_to_periodarr (data , freq , tz )
657
- elif data .dtype == np .int64 :
658
- pass
659
- else :
660
- try :
661
- data = com ._ensure_int64 (data )
662
- except (TypeError , ValueError ):
663
- data = com ._ensure_object (data )
664
- data = _get_ordinals (data , freq )
653
+ if data .dtype != np .int64 :
654
+ if np .issubdtype (data .dtype , np .datetime64 ):
655
+ data = dt64arr_to_periodarr (data , freq , tz )
656
+ else :
657
+ try :
658
+ data = com ._ensure_int64 (data )
659
+ except (TypeError , ValueError ):
660
+ data = com ._ensure_object (data )
661
+ data = _get_ordinals (data , freq )
665
662
666
663
return data , freq
667
664
@@ -1013,8 +1010,7 @@ def join(self, other, how='left', level=None, return_indexers=False):
1013
1010
if return_indexers :
1014
1011
result , lidx , ridx = result
1015
1012
return self ._apply_meta (result ), lidx , ridx
1016
- else :
1017
- return self ._apply_meta (result )
1013
+ return self ._apply_meta (result )
1018
1014
1019
1015
def _assert_can_do_setop (self , other ):
1020
1016
if not isinstance (other , PeriodIndex ):
@@ -1031,9 +1027,10 @@ def _wrap_union_result(self, other, result):
1031
1027
return result
1032
1028
1033
1029
def _apply_meta (self , rawarr ):
1034
- idx = rawarr .view (PeriodIndex )
1035
- idx .freq = self .freq
1036
- return idx
1030
+ if not isinstance (rawarr , PeriodIndex ):
1031
+ rawarr = rawarr .view (PeriodIndex )
1032
+ rawarr .freq = self .freq
1033
+ return rawarr
1037
1034
1038
1035
def __getitem__ (self , key ):
1039
1036
"""Override numpy.ndarray's __getitem__ method to work as desired"""
@@ -1069,18 +1066,19 @@ def _format_native_types(self, na_rep=u('NaT'), **kwargs):
1069
1066
return values .tolist ()
1070
1067
1071
1068
def __array_finalize__ (self , obj ):
1072
- if self .ndim == 0 : # pragma: no cover
1069
+ if not self .ndim : # pragma: no cover
1073
1070
return self .item ()
1074
1071
1075
1072
self .freq = getattr (obj , 'freq' , None )
1076
1073
self .name = getattr (obj , 'name' , None )
1077
1074
1078
1075
def __repr__ (self ):
1079
- output = str (self .__class__ ) + '\n '
1080
- output += 'freq: ' '%s' '\n ' % self .freq
1081
- if len (self ) > 0 :
1076
+ output = com .pprint_thing (self .__class__ ) + '\n '
1077
+ output += 'freq: %s\n ' % self .freq
1078
+ n = len (self )
1079
+ if n :
1082
1080
output += '[%s, ..., %s]\n ' % (self [0 ], self [- 1 ])
1083
- output += 'length: %d' % len ( self )
1081
+ output += 'length: %d' % n
1084
1082
return output
1085
1083
1086
1084
def __unicode__ (self ):
0 commit comments