Skip to content

Commit

Permalink
REF: Consolidate PeriodArray frequency checks (pandas-dev#24285)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger authored Dec 14, 2018
1 parent 4efc819 commit 1e658f7
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@ def wrapper(self, other):
other = other._values

if isinstance(other, Period):
if other.freq != self.freq:
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
raise IncompatibleFrequency(msg)
self._check_compatible_with(other)

result = op(other.ordinal)
elif isinstance(other, cls):
if other.freq != self.freq:
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
raise IncompatibleFrequency(msg)
self._check_compatible_with(other)

if not_implemented:
return NotImplemented
Expand Down Expand Up @@ -241,6 +237,11 @@ def _generate_range(cls, start, end, periods, freq, fields):

return subarr, freq

def _check_compatible_with(self, other):
if self.freqstr != other.freqstr:
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
raise IncompatibleFrequency(msg)

# --------------------------------------------------------------------
# Data / Attributes

Expand Down Expand Up @@ -395,10 +396,7 @@ def _validate_fill_value(self, fill_value):
if isna(fill_value):
fill_value = iNaT
elif isinstance(fill_value, Period):
if fill_value.freq != self.freq:
msg = DIFFERENT_FREQ_INDEX.format(self.freq.freqstr,
fill_value.freqstr)
raise IncompatibleFrequency(msg)
self._check_compatible_with(fill_value)
fill_value = fill_value.ordinal
else:
raise ValueError("'fill_value' should be a Period. "
Expand Down Expand Up @@ -667,10 +665,7 @@ def _sub_datelike(self, other):
def _sub_period(self, other):
# If the operation is well-defined, we return an object-Index
# of DateOffsets. Null entries are filled with pd.NaT
if self.freq != other.freq:
msg = DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr)
raise IncompatibleFrequency(msg)

self._check_compatible_with(other)
asi8 = self.asi8
new_data = asi8 - other.ordinal
new_data = np.array([self.freq * x for x in new_data])
Expand Down

0 comments on commit 1e658f7

Please sign in to comment.