30
30
)
31
31
from pandas .core .dtypes .dtypes import PeriodDtype
32
32
from pandas .core .dtypes .generic import (
33
- ABCSeries , ABCIndex , ABCPeriodIndex
33
+ ABCSeries , ABCPeriodIndex , ABCIndexClass ,
34
34
)
35
35
36
36
import pandas .core .common as com
@@ -63,7 +63,7 @@ def _period_array_cmp(cls, op):
63
63
64
64
def wrapper (self , other ):
65
65
op = getattr (self ._ndarray_values , opname )
66
- if isinstance (other , (ABCSeries , ABCIndex )):
66
+ if isinstance (other , (ABCSeries , ABCIndexClass )):
67
67
other = other .values
68
68
69
69
if isinstance (other , Period ):
@@ -72,7 +72,7 @@ def wrapper(self, other):
72
72
raise IncompatibleFrequency (msg )
73
73
74
74
result = op (other .ordinal )
75
- elif isinstance (other , ( ABCPeriodIndex , cls ) ):
75
+ elif isinstance (other , cls ):
76
76
if other .freq != self .freq :
77
77
msg = DIFFERENT_FREQ_INDEX .format (self .freqstr , other .freqstr )
78
78
raise IncompatibleFrequency (msg )
@@ -88,7 +88,8 @@ def wrapper(self, other):
88
88
result = np .empty (len (self ._ndarray_values ), dtype = bool )
89
89
result .fill (nat_result )
90
90
elif isinstance (other , (list , np .ndarray )):
91
- # XXX: is this correct?
91
+ # XXX: is this correct? Why not convert the
92
+ # sequence to a PeriodArray?
92
93
return NotImplemented
93
94
else :
94
95
other = Period (other , freq = self .freq )
@@ -111,16 +112,16 @@ class PeriodArray(DatetimeLikeArrayMixin, ExtensionArray):
111
112
- ordinals : integer ndarray
112
113
- freq : pd.tseries.offsets.Tick
113
114
114
- The values are physically stored as an ndarray of integers. These are
115
+ The values are physically stored as a 1-D ndarray of integers. These are
115
116
called "ordinals" and represent some kind of offset from a base.
116
117
117
118
The `freq` indicates the span covered by each element of the array.
118
119
All elements in the PeriodArray have the same `freq`.
119
120
"""
120
121
_attributes = ["freq" ]
121
- _typ = "periodarray" # ABCPeriodAray
122
+ _typ = "periodarray" # ABCPeriodArray
122
123
123
- # Names others delegate to us on
124
+ # Names others delegate to us
124
125
_other_ops = []
125
126
_bool_ops = ['is_leap_year' ]
126
127
_object_ops = ['start_time' , 'end_time' , 'freq' ]
@@ -134,7 +135,7 @@ class PeriodArray(DatetimeLikeArrayMixin, ExtensionArray):
134
135
# --------------------------------------------------------------------
135
136
# Constructors
136
137
def __init__ (self , values , freq = None ):
137
- # type: (np.ndarray[int64], Union[str, Tick]) -> None
138
+ # type: (np.ndarray[np. int64], Union[str, Tick]) -> None
138
139
values = np .array (values , dtype = 'int64' , copy = False )
139
140
self ._data = values
140
141
if freq is None :
@@ -237,7 +238,7 @@ def _from_ordinals(cls, values, freq=None):
237
238
# type: (ndarray[int], Optional[Tick]) -> PeriodArray
238
239
"""
239
240
Values should be int ordinals
240
- `__new__` & `_simple_new` cooerce to ordinals and call this method
241
+ `__new__` & `_simple_new` coerce to ordinals and call this method
241
242
"""
242
243
return cls (values , freq = freq )
243
244
@@ -536,7 +537,7 @@ def _add_offset(self, other):
536
537
if base != self .freq .rule_code :
537
538
msg = DIFFERENT_FREQ_INDEX .format (self .freqstr , other .freqstr )
538
539
raise IncompatibleFrequency (msg )
539
- return self .shift (other .n )
540
+ return self ._tshift (other .n )
540
541
541
542
def _add_delta_td (self , other ):
542
543
assert isinstance (other , (timedelta , np .timedelta64 , Tick ))
@@ -546,7 +547,7 @@ def _add_delta_td(self, other):
546
547
if isinstance (own_offset , Tick ):
547
548
offset_nanos = delta_to_nanoseconds (own_offset )
548
549
if np .all (nanos % offset_nanos == 0 ):
549
- return self .shift (nanos // offset_nanos )
550
+ return self ._tshift (nanos // offset_nanos )
550
551
551
552
# raise when input doesn't have freq
552
553
raise IncompatibleFrequency ("Input has different freq from "
@@ -556,7 +557,7 @@ def _add_delta_td(self, other):
556
557
557
558
def _add_delta (self , other ):
558
559
ordinal_delta = self ._maybe_convert_timedelta (other )
559
- return self .shift (ordinal_delta )
560
+ return self ._tshift (ordinal_delta )
560
561
561
562
def shift (self , periods = 1 ):
562
563
"""
@@ -640,6 +641,7 @@ def _maybe_convert_timedelta(self, other):
640
641
freqstr = self .freqstr ))
641
642
642
643
def _format_native_types (self , na_rep = u'NaT' , date_format = None ):
644
+ # TODO(DatetimeArray): remove
643
645
values = self .astype (object )
644
646
645
647
if date_format :
@@ -658,7 +660,8 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None):
658
660
return values
659
661
660
662
def view (self , dtype = None , type = None ):
661
- # This is to support PeriodIndex.view('i8')
663
+ # This is to support things like `.asi8`
664
+ # PeriodIndex's parent does .values.view('i8').
662
665
# I don't like adding this,
663
666
return self ._data .view (dtype = dtype )
664
667
@@ -757,6 +760,28 @@ def _box_values_as_index(self):
757
760
from pandas .core .index import Index
758
761
return Index (self ._box_values (self .asi8 ), dtype = object )
759
762
763
+ @property
764
+ def flags (self ):
765
+ """Deprecated"""
766
+ # Just here to support Index.flags deprecation.
767
+ # could also override PeriodIndex.flags if we don't want a
768
+ # version with PeriodArray.flags
769
+ return self .values .flags
770
+
771
+ @property
772
+ def base (self ):
773
+ return self .values .base
774
+
775
+ @property
776
+ def data (self ):
777
+ return self .astype (object ).data
778
+
779
+ def item (self ):
780
+ if len (self ) == 1 :
781
+ return Period ._from_ordinal (self .values [0 ], self .freq )
782
+ else :
783
+ raise ValueError ('can only convert an array of size 1 to a '
784
+ 'Python scalar' )
760
785
761
786
PeriodArray ._add_comparison_ops ()
762
787
PeriodArray ._add_datetimelike_methods ()
0 commit comments