@@ -553,11 +553,9 @@ class PeriodIndex(Int64Index):
553553 __le__ = _period_index_cmp ('__le__' )
554554 __ge__ = _period_index_cmp ('__ge__' )
555555
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 ,
561559 tz = None ):
562560
563561 freq = _freq_mod .get_standard_freq (freq )
@@ -649,19 +647,18 @@ def _from_arraylike(cls, data, freq, tz):
649647 freq = getattr (data [0 ], 'freq' , None )
650648
651649 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' )
654652
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 )
665662
666663 return data , freq
667664
@@ -1013,8 +1010,7 @@ def join(self, other, how='left', level=None, return_indexers=False):
10131010 if return_indexers :
10141011 result , lidx , ridx = result
10151012 return self ._apply_meta (result ), lidx , ridx
1016- else :
1017- return self ._apply_meta (result )
1013+ return self ._apply_meta (result )
10181014
10191015 def _assert_can_do_setop (self , other ):
10201016 if not isinstance (other , PeriodIndex ):
@@ -1031,9 +1027,10 @@ def _wrap_union_result(self, other, result):
10311027 return result
10321028
10331029 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
10371034
10381035 def __getitem__ (self , key ):
10391036 """Override numpy.ndarray's __getitem__ method to work as desired"""
@@ -1069,18 +1066,19 @@ def _format_native_types(self, na_rep=u('NaT'), **kwargs):
10691066 return values .tolist ()
10701067
10711068 def __array_finalize__ (self , obj ):
1072- if self .ndim == 0 : # pragma: no cover
1069+ if not self .ndim : # pragma: no cover
10731070 return self .item ()
10741071
10751072 self .freq = getattr (obj , 'freq' , None )
10761073 self .name = getattr (obj , 'name' , None )
10771074
10781075 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 :
10821080 output += '[%s, ..., %s]\n ' % (self [0 ], self [- 1 ])
1083- output += 'length: %d' % len ( self )
1081+ output += 'length: %d' % n
10841082 return output
10851083
10861084 def __unicode__ (self ):
0 commit comments