8
8
is_integer ,
9
9
is_float ,
10
10
is_integer_dtype ,
11
- is_float_dtype ,
12
11
is_scalar ,
13
12
is_datetime64_dtype ,
14
13
is_datetime64_any_dtype ,
@@ -171,15 +170,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
171
170
if name is None and hasattr (data , 'name' ):
172
171
name = data .name
173
172
174
- if dtype is not None :
175
- dtype = pandas_dtype (dtype )
176
- if not is_period_dtype (dtype ):
177
- raise ValueError ('dtype must be PeriodDtype' )
178
- if freq is None :
179
- freq = dtype .freq
180
- elif freq != dtype .freq :
181
- msg = 'specified freq and dtype are different'
182
- raise IncompatibleFrequency (msg )
173
+ freq = dtl .validate_dtype_freq (dtype , freq )
183
174
184
175
# coerce freq to freq object, otherwise it can be coerced elementwise
185
176
# which is slow
@@ -192,7 +183,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
192
183
else :
193
184
data , freq = cls ._generate_range (start , end , periods ,
194
185
freq , fields )
195
- return cls ._from_ordinals (data , name = name , freq = freq )
186
+ return cls ._simple_new (data , name = name , freq = freq )
196
187
197
188
if isinstance (data , PeriodIndex ):
198
189
if freq is None or freq == data .freq : # no freq change
@@ -208,7 +199,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
208
199
# not array / index
209
200
if not isinstance (data , (np .ndarray , PeriodIndex ,
210
201
DatetimeIndex , Int64Index )):
211
- if is_scalar (data ) or isinstance ( data , Period ) :
202
+ if is_scalar (data ):
212
203
cls ._scalar_data_error (data )
213
204
214
205
# other iterable of some kind
@@ -220,7 +211,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
220
211
# datetime other than period
221
212
if is_datetime64_dtype (data .dtype ):
222
213
data = dt64arr_to_periodarr (data , freq , tz )
223
- return cls ._from_ordinals (data , name = name , freq = freq )
214
+ return cls ._simple_new (data , name = name , freq = freq )
224
215
225
216
# check not floats
226
217
if infer_dtype (data ) == 'floating' and len (data ) > 0 :
@@ -231,33 +222,15 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
231
222
data = ensure_object (data )
232
223
freq = freq or period .extract_freq (data )
233
224
data = period .extract_ordinals (data , freq )
234
- return cls ._from_ordinals (data , name = name , freq = freq )
225
+ return cls ._simple_new (data , name = name , freq = freq )
235
226
236
227
@cache_readonly
237
228
def _engine (self ):
238
229
return self ._engine_type (lambda : self , len (self ))
239
230
240
231
@classmethod
241
- def _simple_new (cls , values , name = None , freq = None , ** kwargs ):
242
- """
243
- Values can be any type that can be coerced to Periods.
244
- Ordinals in an ndarray are fastpath-ed to `_from_ordinals`
245
- """
246
- if not is_integer_dtype (values ):
247
- values = np .array (values , copy = False )
248
- if len (values ) > 0 and is_float_dtype (values ):
249
- raise TypeError ("PeriodIndex can't take floats" )
250
- return cls (values , name = name , freq = freq , ** kwargs )
251
-
252
- return cls ._from_ordinals (values , name , freq , ** kwargs )
253
-
254
- @classmethod
255
- def _from_ordinals (cls , values , name = None , freq = None , ** kwargs ):
256
- """
257
- Values should be int ordinals
258
- `__new__` & `_simple_new` cooerce to ordinals and call this method
259
- """
260
- result = super (PeriodIndex , cls )._from_ordinals (values , freq )
232
+ def _simple_new (cls , values , freq = None , name = None , ** kwargs ):
233
+ result = super (PeriodIndex , cls )._simple_new (values , freq )
261
234
262
235
result .name = name
263
236
result ._reset_identity ()
@@ -702,8 +675,8 @@ def _wrap_union_result(self, other, result):
702
675
703
676
def _apply_meta (self , rawarr ):
704
677
if not isinstance (rawarr , PeriodIndex ):
705
- rawarr = PeriodIndex ._from_ordinals (rawarr , freq = self .freq ,
706
- name = self .name )
678
+ rawarr = PeriodIndex ._simple_new (rawarr , freq = self .freq ,
679
+ name = self .name )
707
680
return rawarr
708
681
709
682
def _format_native_types (self , na_rep = u'NaT' , date_format = None , ** kwargs ):
0 commit comments