Skip to content

Commit f9a05e3

Browse files
committed
documentation
1 parent 549474d commit f9a05e3

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
@@ -213,7 +213,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
213213
return cls._from_ordinals(data, name=name, freq=freq)
214214

215215
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
217217
freq = data.freq
218218
data = data._values
219219
else:
@@ -223,6 +223,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
223223
base1, base2, 1)
224224
return cls._simple_new(data, name=name, freq=freq)
225225

226+
# not array / index
226227
if not isinstance(data, (np.ndarray, PeriodIndex,
227228
DatetimeIndex, Int64Index)):
228229
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,
236237

237238
data = np.asarray(data)
238239

240+
# datetime other than period
239241
if np.issubdtype(data.dtype, np.datetime64):
240242
data = dt64arr_to_periodarr(data, freq, tz)
241243
return cls._from_ordinals(data, name=name, freq=freq)
242244

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

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

254255
@classmethod
255256
def _generate_range(cls, start, end, periods, freq, fields):
@@ -272,10 +273,13 @@ def _generate_range(cls, start, end, periods, freq, fields):
272273

273274
@classmethod
274275
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+
"""
276280
if not is_integer_dtype(values):
277281
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):
279283
raise TypeError("PeriodIndex can't take floats")
280284
else:
281285
return cls(values, name=name, freq=freq, **kwargs)
@@ -284,6 +288,10 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
284288

285289
@classmethod
286290
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+
"""
287295

288296
values = np.array(values, dtype='int64', copy=False)
289297

0 commit comments

Comments
 (0)