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