Skip to content

Commit

Permalink
REF: easy parts of pandas-dev#23416
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Oct 30, 2018
1 parent 7191af9 commit 1573d21
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 29 deletions.
3 changes: 3 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
31 changes: 2 additions & 29 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1573d21

Please sign in to comment.