Skip to content

CLN: small clean-up PeriodIndex (easy parts of #23416) #23423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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