21
21
# Period logic
22
22
23
23
24
- def _period_field_accessor (name , alias = None ):
25
- if alias is None :
26
- alias = name
24
+ def _period_field_accessor (name , alias ):
27
25
def f (self ):
28
26
base , mult = _gfc (self .freq )
29
27
return plib .get_period_field (alias , self .ordinal , base )
30
28
f .__name__ = name
31
29
return property (f )
32
30
33
- def _field_accessor (name , alias = None ):
34
- if alias is None :
35
- alias = name
31
+ def _field_accessor (name , alias ):
36
32
def f (self ):
37
33
base , mult = _gfc (self .freq )
38
34
return plib .get_period_field_arr (alias , self .values , base )
39
35
f .__name__ = name
40
36
return property (f )
41
37
42
- def to_period (arg , freq = None ):
43
- """ Attempts to convert arg to timestamp """
44
- if arg is None :
45
- return arg
46
-
47
- if type (arg ) == float :
48
- raise TypeError ("Cannot convert a float to period" )
49
-
50
- return Period (arg , freq = freq )
51
-
52
- def _to_quarterly (year , month , freq = 'Q-DEC' ):
53
- fmonth = _freq_mod ._month_numbers [_freq_mod ._get_rule_month (freq )] + 1
54
- print fmonth
55
- mdiff = (month - fmonth ) % 12
56
- if month >= fmonth :
57
- mdiff += 12
58
-
59
- ordin = 1 + (year - 1 ) * 4 + (mdiff - 1 ) / 3
60
- return Period (ordin , freq = freq )
61
-
62
38
class Period (object ):
63
39
64
40
__slots__ = ['freq' , 'ordinal' ]
@@ -106,9 +82,6 @@ def __init__(self, value=None, freq=None, ordinal=None,
106
82
if freq is None :
107
83
raise ValueError ("If value is None, freq cannot be None" )
108
84
109
- if year is None :
110
- raise ValueError ("If value is None, year cannot be None" )
111
-
112
85
self .ordinal = _ordinal_from_fields (year , month , quarter , day ,
113
86
hour , minute , second , freq )
114
87
@@ -172,13 +145,16 @@ def __sub__(self, other):
172
145
else : # pragma: no cover
173
146
raise TypeError (other )
174
147
175
- def asfreq (self , freq = None , how = 'E' ):
148
+ def asfreq (self , freq , how = 'E' ):
176
149
"""
150
+ Convert Period to desired frequency, either at the start or end of the
151
+ interval
177
152
178
153
Parameters
179
154
----------
180
- freq :
181
- how :
155
+ freq : string
156
+ how : {'E', 'S', 'end', 'start'}, default 'end'
157
+ Start or end of the timespan
182
158
183
159
Returns
184
160
-------
@@ -220,17 +196,13 @@ def to_timestamp(self, freq=None, how='S'):
220
196
-------
221
197
Timestamp
222
198
"""
223
- # how = _validate_end_alias(how)
224
199
if freq is None :
225
200
base , mult = _gfc (self .freq )
226
201
new_val = self
227
202
else :
228
203
base , mult = _gfc (freq )
229
204
new_val = self .asfreq (freq , how )
230
205
231
- if mult != 1 :
232
- raise ValueError ('Only mult == 1 supported' )
233
-
234
206
dt64 = plib .period_ordinal_to_dt64 (new_val .ordinal , base )
235
207
ts_freq = _period_rule_to_timestamp_rule (new_val .freq , how = how )
236
208
return Timestamp (dt64 , offset = to_offset (ts_freq ))
@@ -424,7 +396,7 @@ def _get_date_and_freq(value, freq):
424
396
elif reso == 'second' :
425
397
freq = 'S'
426
398
else :
427
- raise ValueError ("Could not infer frequency for period" )
399
+ raise ValueError ("Invalid frequency or could not infer: %s" % reso )
428
400
429
401
return dt , freq
430
402
@@ -444,11 +416,6 @@ def _period_unbox_array(arr, check=None):
444
416
unboxer = np .frompyfunc (lambda x : _period_unbox (x , check = check ), 1 , 1 )
445
417
return unboxer (arr )
446
418
447
- def _period_box_array (arr , freq ):
448
- boxfunc = lambda x : Period (ordinal = x , freq = freq )
449
- boxer = np .frompyfunc (boxfunc , 1 , 1 )
450
- return boxer (arr )
451
-
452
419
def dt64arr_to_periodarr (data , freq ):
453
420
if data .dtype != np .dtype ('M8[ns]' ):
454
421
raise ValueError ('Wrong dtype: %s' % data .dtype )
@@ -479,7 +446,10 @@ def wrapper(self, other):
479
446
return result
480
447
return wrapper
481
448
449
+
482
450
_INT64_DTYPE = np .dtype (np .int64 )
451
+ _NS_DTYPE = np .dtype ('M8[ns]' )
452
+
483
453
484
454
class PeriodIndex (Int64Index ):
485
455
"""
@@ -730,12 +700,18 @@ def map(self, f):
730
700
try :
731
701
return f (self )
732
702
except :
733
- values = np . asarray ( list ( self ), dtype = object )
703
+ values = self . _get_object_array ( )
734
704
return _algos .arrmap_object (values , f )
735
705
706
+ def _get_object_array (self ):
707
+ freq = self .freq
708
+ boxfunc = lambda x : Period (ordinal = x , freq = freq )
709
+ boxer = np .frompyfunc (boxfunc , 1 , 1 )
710
+ return boxer (self .values )
711
+
736
712
def _mpl_repr (self ):
737
713
# how to represent ourselves to matplotlib
738
- return _period_box_array ( self , self . freq )
714
+ return self . _get_object_array ( )
739
715
740
716
def to_timestamp (self , freq = None , how = 'start' ):
741
717
"""
@@ -758,9 +734,6 @@ def to_timestamp(self, freq=None, how='start'):
758
734
base , mult = _gfc (freq )
759
735
new_data = self .asfreq (freq , how )
760
736
761
- if mult != 1 :
762
- raise ValueError ('Only mult == 1 supported' )
763
-
764
737
new_data = plib .periodarr_to_dt64arr (new_data .values , base )
765
738
return DatetimeIndex (new_data , freq = 'infer' , name = self .name )
766
739
@@ -823,14 +796,14 @@ def get_value(self, series, key):
823
796
key = slice (pos [0 ], pos [1 ]+ 1 )
824
797
return series [key ]
825
798
else :
826
- key = to_period (asdt , freq = self .freq )
799
+ key = Period (asdt , freq = self .freq )
827
800
return self ._engine .get_value (series , key .ordinal )
828
801
except TypeError :
829
802
pass
830
803
except KeyError :
831
804
pass
832
805
833
- key = to_period (key , self .freq )
806
+ key = Period (key , self .freq )
834
807
return self ._engine .get_value (series , key .ordinal )
835
808
836
809
def get_loc (self , key ):
@@ -850,7 +823,7 @@ def get_loc(self, key):
850
823
except TypeError :
851
824
pass
852
825
853
- key = to_period (key , self .freq ).ordinal
826
+ key = Period (key , self .freq ).ordinal
854
827
return self ._engine .get_loc (key )
855
828
856
829
def join (self , other , how = 'left' , level = None , return_indexers = False ):
@@ -946,8 +919,10 @@ def _get_ordinal_range(start, end, periods, freq):
946
919
if com ._count_not_none (start , end , periods ) < 2 :
947
920
raise ValueError ('Must specify 2 of start, end, periods' )
948
921
949
- start = to_period (start , freq )
950
- end = to_period (end , freq )
922
+ if start is not None :
923
+ start = Period (start , freq )
924
+ if end is not None :
925
+ end = Period (end , freq )
951
926
952
927
is_start_per = isinstance (start , Period )
953
928
is_end_per = isinstance (end , Period )
0 commit comments