Skip to content

Commit 8b9600c

Browse files
authored
REF: avoid Period.freq (#52549)
* REF: avoid Period.freq * typo fixup * mypy fixup
1 parent bfa5f9b commit 8b9600c

File tree

5 files changed

+14
-23
lines changed

5 files changed

+14
-23
lines changed

pandas/_libs/tslibs/period.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from typing import Literal
33

44
import numpy as np
55

6+
from pandas._libs.tslibs.dtypes import PeriodDtypeBase
67
from pandas._libs.tslibs.nattype import NaTType
78
from pandas._libs.tslibs.offsets import BaseOffset
89
from pandas._libs.tslibs.timestamps import Timestamp
@@ -67,6 +68,7 @@ class PeriodMixin:
6768
class Period(PeriodMixin):
6869
ordinal: int # int64_t
6970
freq: BaseOffset
71+
_dtype: PeriodDtypeBase
7072

7173
# error: "__new__" must return a class instance (got "Union[Period, NaTType]")
7274
def __new__( # type: ignore[misc]

pandas/_libs/tslibs/period.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ cdef class _Period(PeriodMixin):
17111711

17121712
def __richcmp__(self, other, op):
17131713
if is_period_object(other):
1714-
if other.freq != self.freq:
1714+
if other._dtype != self._dtype:
17151715
if op == Py_EQ:
17161716
return False
17171717
elif op == Py_NE:
@@ -1781,7 +1781,7 @@ cdef class _Period(PeriodMixin):
17811781
elif other is NaT:
17821782
return NaT
17831783
elif util.is_integer_object(other):
1784-
ordinal = self.ordinal + other * self.freq.n
1784+
ordinal = self.ordinal + other * self._dtype._n
17851785
return Period(ordinal=ordinal, freq=self.freq)
17861786

17871787
elif is_period_object(other):
@@ -1866,7 +1866,7 @@ cdef class _Period(PeriodMixin):
18661866
# self.n can't be negative or 0
18671867
end = how == "E"
18681868
if end:
1869-
ordinal = self.ordinal + self.freq.n - 1
1869+
ordinal = self.ordinal + self._dtype._n - 1
18701870
else:
18711871
ordinal = self.ordinal
18721872
ordinal = period_asfreq(ordinal, base1, base2, end)
@@ -2630,7 +2630,7 @@ class Period(_Period):
26302630

26312631
elif is_period_object(value):
26322632
other = value
2633-
if freq is None or freq._period_dtype_code == other.freq._period_dtype_code:
2633+
if freq is None or freq._period_dtype_code == other._dtype._dtype_code:
26342634
ordinal = other.ordinal
26352635
freq = other.freq
26362636
else:

pandas/core/arrays/datetimelike.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ def __add__(self, other):
13091309
if not isinstance(self.dtype, PeriodDtype):
13101310
raise integer_op_not_supported(self)
13111311
obj = cast("PeriodArray", self)
1312-
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.add)
1312+
result = obj._addsub_int_array_or_scalar(other * obj.dtype._n, operator.add)
13131313

13141314
# array-like others
13151315
elif is_timedelta64_dtype(other_dtype):
@@ -1327,7 +1327,7 @@ def __add__(self, other):
13271327
if not isinstance(self.dtype, PeriodDtype):
13281328
raise integer_op_not_supported(self)
13291329
obj = cast("PeriodArray", self)
1330-
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.add)
1330+
result = obj._addsub_int_array_or_scalar(other * obj.dtype._n, operator.add)
13311331
else:
13321332
# Includes Categorical, other ExtensionArrays
13331333
# For PeriodDtype, if self is a TimedeltaArray and other is a
@@ -1367,7 +1367,7 @@ def __sub__(self, other):
13671367
if not isinstance(self.dtype, PeriodDtype):
13681368
raise integer_op_not_supported(self)
13691369
obj = cast("PeriodArray", self)
1370-
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.sub)
1370+
result = obj._addsub_int_array_or_scalar(other * obj.dtype._n, operator.sub)
13711371

13721372
elif isinstance(other, Period):
13731373
result = self._sub_periodlike(other)
@@ -1391,7 +1391,7 @@ def __sub__(self, other):
13911391
if not isinstance(self.dtype, PeriodDtype):
13921392
raise integer_op_not_supported(self)
13931393
obj = cast("PeriodArray", self)
1394-
result = obj._addsub_int_array_or_scalar(other * obj.freq.n, operator.sub)
1394+
result = obj._addsub_int_array_or_scalar(other * obj.dtype._n, operator.sub)
13951395
else:
13961396
# Includes ExtensionArrays, float_dtype
13971397
return NotImplemented

pandas/core/arrays/period.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
def _field_accessor(name: str, docstring: str | None = None):
106106
def f(self):
107-
base = self.freq._period_dtype_code
107+
base = self.dtype._dtype_code
108108
result = get_period_field_arr(name, self.asi8, base)
109109
return result
110110

@@ -543,7 +543,7 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
543543
diffs = libalgos.unique_deltas(self.asi8)
544544
if len(diffs) == 1:
545545
diff = diffs[0]
546-
if diff == self.freq.n:
546+
if diff == self.dtype._n:
547547
dta._freq = self.freq
548548
elif diff == 1:
549549
dta._freq = self.freq.base
@@ -614,7 +614,7 @@ def asfreq(self, freq=None, how: str = "E") -> Self:
614614
# self.freq.n can't be negative or 0
615615
end = how == "E"
616616
if end:
617-
ordinal = asi8 + self.freq.n - 1
617+
ordinal = asi8 + self.dtype._n - 1
618618
else:
619619
ordinal = asi8
620620

pandas/core/indexes/period.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,7 @@ def get_loc(self, key):
435435
raise KeyError(orig_key) from err
436436

437437
def _disallow_mismatched_indexing(self, key: Period) -> None:
438-
sfreq = self.freq
439-
kfreq = key.freq
440-
if not (
441-
sfreq.n == kfreq.n
442-
# error: "BaseOffset" has no attribute "_period_dtype_code"
443-
and sfreq._period_dtype_code # type: ignore[attr-defined]
444-
# error: "BaseOffset" has no attribute "_period_dtype_code"
445-
== kfreq._period_dtype_code # type: ignore[attr-defined]
446-
):
447-
# GH#42247 For the subset of DateOffsets that can be Period freqs,
448-
# checking these two attributes is sufficient to check equality,
449-
# and much more performant than `self.freq == key.freq`
438+
if key._dtype != self.dtype:
450439
raise KeyError(key)
451440

452441
def _cast_partial_indexing_scalar(self, label: datetime) -> Period:

0 commit comments

Comments
 (0)