@@ -217,7 +217,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
217
217
return cls ._from_ordinals (data , name = name , freq = freq )
218
218
219
219
if isinstance (data , PeriodIndex ):
220
- if freq is None or freq == data .freq :
220
+ if freq is None or freq == data .freq : # no freq change
221
221
freq = data .freq
222
222
data = data ._values
223
223
else :
@@ -227,6 +227,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
227
227
base1 , base2 , 1 )
228
228
return cls ._simple_new (data , name = name , freq = freq )
229
229
230
+ # not array / index
230
231
if not isinstance (data , (np .ndarray , PeriodIndex ,
231
232
DatetimeIndex , Int64Index )):
232
233
if is_scalar (data ) or isinstance (data , Period ):
@@ -240,20 +241,20 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
240
241
241
242
data = np .asarray (data )
242
243
244
+ # datetime other than period
243
245
if np .issubdtype (data .dtype , np .datetime64 ):
244
246
data = dt64arr_to_periodarr (data , freq , tz )
245
247
return cls ._from_ordinals (data , name = name , freq = freq )
246
248
247
- inferred_dtype = infer_dtype (data )
248
-
249
- if inferred_dtype == 'floating' and len (data ) > 0 :
249
+ # check not floats
250
+ if infer_dtype (data ) == 'floating' and len (data ) > 0 :
250
251
raise TypeError ("PeriodIndex can't take floats" )
251
252
252
- else :
253
- data = _ensure_object (data )
254
- freq = freq or period .extract_freq (data )
255
- data = period .extract_ordinals (data , freq )
256
- return cls ._from_ordinals (data , name = name , freq = freq )
253
+ # anything else, likely an array of strings or periods
254
+ data = _ensure_object (data )
255
+ freq = freq or period .extract_freq (data )
256
+ data = period .extract_ordinals (data , freq )
257
+ return cls ._from_ordinals (data , name = name , freq = freq )
257
258
258
259
@classmethod
259
260
def _generate_range (cls , start , end , periods , freq , fields ):
@@ -276,10 +277,13 @@ def _generate_range(cls, start, end, periods, freq, fields):
276
277
277
278
@classmethod
278
279
def _simple_new (cls , values , name = None , freq = None , ** kwargs ):
279
-
280
+ """
281
+ Values can be any type that can be coerced to Periods.
282
+ Ordinals in an ndarray are fastpath-ed to `_from_ordinals`
283
+ """
280
284
if not is_integer_dtype (values ):
281
285
values = np .array (values , copy = False )
282
- if ( len (values ) > 0 and is_float_dtype (values ) ):
286
+ if len (values ) > 0 and is_float_dtype (values ):
283
287
raise TypeError ("PeriodIndex can't take floats" )
284
288
else :
285
289
return cls (values , name = name , freq = freq , ** kwargs )
@@ -288,6 +292,10 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
288
292
289
293
@classmethod
290
294
def _from_ordinals (cls , values , name = None , freq = None , ** kwargs ):
295
+ """
296
+ Values should be int ordinals
297
+ `__new__` & `_simple_new` cooerce to ordinals and call this method
298
+ """
291
299
292
300
values = np .array (values , dtype = 'int64' , copy = False )
293
301
0 commit comments