Skip to content

Commit ba5133b

Browse files
committed
documentation
1 parent b0fc0a7 commit ba5133b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

pandas/tseries/period.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
217217
return cls._from_ordinals(data, name=name, freq=freq)
218218

219219
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
221221
freq = data.freq
222222
data = data._values
223223
else:
@@ -227,6 +227,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
227227
base1, base2, 1)
228228
return cls._simple_new(data, name=name, freq=freq)
229229

230+
# not array / index
230231
if not isinstance(data, (np.ndarray, PeriodIndex,
231232
DatetimeIndex, Int64Index)):
232233
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,
240241

241242
data = np.asarray(data)
242243

244+
# datetime other than period
243245
if np.issubdtype(data.dtype, np.datetime64):
244246
data = dt64arr_to_periodarr(data, freq, tz)
245247
return cls._from_ordinals(data, name=name, freq=freq)
246248

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:
250251
raise TypeError("PeriodIndex can't take floats")
251252

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)
257258

258259
@classmethod
259260
def _generate_range(cls, start, end, periods, freq, fields):
@@ -276,10 +277,13 @@ def _generate_range(cls, start, end, periods, freq, fields):
276277

277278
@classmethod
278279
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+
"""
280284
if not is_integer_dtype(values):
281285
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):
283287
raise TypeError("PeriodIndex can't take floats")
284288
else:
285289
return cls(values, name=name, freq=freq, **kwargs)
@@ -288,6 +292,10 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
288292

289293
@classmethod
290294
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+
"""
291299

292300
values = np.array(values, dtype='int64', copy=False)
293301

0 commit comments

Comments
 (0)