Skip to content

Commit 4e2cb22

Browse files
authored
TYP: require_matching_freq (#56374)
* TYP: require_matching_freq * mypy fixup
1 parent 9f51d4f commit 4e2cb22

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

pandas/_libs/tslibs/period.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class PeriodMixin:
6363
def end_time(self) -> Timestamp: ...
6464
@property
6565
def start_time(self) -> Timestamp: ...
66-
def _require_matching_freq(self, other, base: bool = ...) -> None: ...
66+
def _require_matching_freq(self, other: BaseOffset, base: bool = ...) -> None: ...
6767

6868
class Period(PeriodMixin):
6969
ordinal: int # int64_t

pandas/_libs/tslibs/period.pyx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,21 +1714,16 @@ cdef class PeriodMixin:
17141714
"""
17151715
return self.to_timestamp(how="end")
17161716

1717-
def _require_matching_freq(self, other, base=False):
1717+
def _require_matching_freq(self, other: BaseOffset, bint base=False):
17181718
# See also arrays.period.raise_on_incompatible
1719-
if is_offset_object(other):
1720-
other_freq = other
1721-
else:
1722-
other_freq = other.freq
1723-
17241719
if base:
1725-
condition = self.freq.base != other_freq.base
1720+
condition = self.freq.base != other.base
17261721
else:
1727-
condition = self.freq != other_freq
1722+
condition = self.freq != other
17281723

17291724
if condition:
17301725
freqstr = freq_to_period_freqstr(self.freq.n, self.freq.name)
1731-
other_freqstr = freq_to_period_freqstr(other_freq.n, other_freq.name)
1726+
other_freqstr = freq_to_period_freqstr(other.n, other.name)
17321727
msg = DIFFERENT_FREQ.format(
17331728
cls=type(self).__name__,
17341729
own_freq=freqstr,
@@ -1803,7 +1798,7 @@ cdef class _Period(PeriodMixin):
18031798
return False
18041799
elif op == Py_NE:
18051800
return True
1806-
self._require_matching_freq(other)
1801+
self._require_matching_freq(other.freq)
18071802
return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)
18081803
elif other is NaT:
18091804
return op == Py_NE
@@ -1893,7 +1888,7 @@ cdef class _Period(PeriodMixin):
18931888
):
18941889
return self + (-other)
18951890
elif is_period_object(other):
1896-
self._require_matching_freq(other)
1891+
self._require_matching_freq(other.freq)
18971892
# GH 23915 - mul by base freq since __add__ is agnostic of n
18981893
return (self.ordinal - other.ordinal) * self.freq.base
18991894
elif other is NaT:

pandas/core/arrays/period.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,15 @@ def _unbox_scalar( # type: ignore[override]
370370
def _scalar_from_string(self, value: str) -> Period:
371371
return Period(value, freq=self.freq)
372372

373-
def _check_compatible_with(self, other) -> None:
373+
# error: Argument 1 of "_check_compatible_with" is incompatible with
374+
# supertype "DatetimeLikeArrayMixin"; supertype defines the argument type
375+
# as "Period | Timestamp | Timedelta | NaTType"
376+
def _check_compatible_with(self, other: Period | NaTType | PeriodArray) -> None: # type: ignore[override]
374377
if other is NaT:
375378
return
376-
self._require_matching_freq(other)
379+
# error: Item "NaTType" of "Period | NaTType | PeriodArray" has no
380+
# attribute "freq"
381+
self._require_matching_freq(other.freq) # type: ignore[union-attr]
377382

378383
# --------------------------------------------------------------------
379384
# Data / Attributes

0 commit comments

Comments
 (0)