diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 0247ce8dc6ac4..dc7cf51ca109d 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -944,6 +944,9 @@ def validate_dtype_freq(dtype, freq): ValueError : non-period dtype IncompatibleFrequency : mismatch between dtype and freq """ + if freq is not None: + freq = frequencies.to_offset(freq) + if dtype is not None: dtype = pandas_dtype(dtype) if not is_period_dtype(dtype): diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index c3728d8d956de..ae5c3ddc9dfb4 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -29,7 +29,7 @@ from pandas._libs.tslibs import resolution from pandas.core.algorithms import unique1d -from pandas.core.dtypes.dtypes import PeriodDtype +import pandas.core.arrays.datetimelike as dtl from pandas.core.arrays.period import PeriodArray, period_array from pandas.core.base import _shared_docs from pandas.core.indexes.base import _index_shared_docs, ensure_index @@ -48,17 +48,6 @@ dict(target_klass='PeriodIndex or list of Periods')) -def _wrap_field_accessor(name): - fget = getattr(PeriodArray, name).fget - - def f(self): - result = fget(self) - return Index(result, name=self.name) - - f.__name__ = name - f.__doc__ = fget.__doc__ - return property(f) - # --- Period index sketch @@ -211,27 +200,11 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None, if data is None and ordinal is None: # range-based. - if periods is not None: - if is_float(periods): - periods = int(periods) - - elif not is_integer(periods): - msg = 'periods must be a number, got {periods}' - raise TypeError(msg.format(periods=periods)) - data, freq = PeriodArray._generate_range(start, end, periods, freq, fields) data = PeriodArray(data, freq=freq) else: - if freq is None and dtype is not None: - freq = PeriodDtype(dtype).freq - elif freq and dtype: - freq = PeriodDtype(freq).freq - dtype = PeriodDtype(dtype).freq - - if freq != dtype: - msg = "specified freq and dtype are different" - raise IncompatibleFrequency(msg) + freq = dtl.validate_dtype_freq(dtype, freq) # PeriodIndex allow PeriodIndex(period_index, freq=different) # Let's not encourage that kind of behavior in PeriodArray.