Skip to content

Commit e616938

Browse files
authored
REF: use dtype.freq less in PeriodDtype (#52585)
* REF: use dtype.freq less in PeriodDtype * mypy fixup
1 parent f70638f commit e616938

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

pandas/_libs/tslibs/dtypes.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ cdef class PeriodDtypeBase:
5353
@property
5454
def _freqstr(self) -> str:
5555
# Will be passed to to_offset in Period._maybe_convert_freq
56-
return _reverse_period_code_map.get(self._dtype_code)
56+
out = _reverse_period_code_map.get(self._dtype_code)
57+
if self._n == 1:
58+
return out
59+
return str(self._n) + out
5760

5861
cpdef int _get_to_timestamp_base(self):
5962
"""

pandas/_libs/tslibs/period.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,8 @@ cdef class _Period(PeriodMixin):
16881688
# We already have a dtype code
16891689
dtype = PeriodDtypeBase(freq, 1)
16901690
freq = dtype._freqstr
1691+
elif isinstance(freq, PeriodDtypeBase):
1692+
freq = freq._freqstr
16911693

16921694
freq = to_offset(freq)
16931695

@@ -2372,7 +2374,7 @@ cdef class _Period(PeriodMixin):
23722374
"""
23732375
Return a string representation of the frequency.
23742376
"""
2375-
return self.freq.freqstr
2377+
return self._dtype._freqstr
23762378

23772379
def __repr__(self) -> str:
23782380
base = self._dtype._dtype_code

pandas/core/dtypes/dtypes.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype):
863863
_match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
864864
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
865865
__hash__ = PeriodDtypeBase.__hash__
866+
_freq: BaseOffset
866867

867868
def __new__(cls, freq):
868869
"""
@@ -886,7 +887,7 @@ def __new__(cls, freq):
886887
return u
887888

888889
def __reduce__(self):
889-
return type(self), (self.freq,)
890+
return type(self), (self.name,)
890891

891892
@property
892893
def freq(self):
@@ -940,7 +941,7 @@ def __str__(self) -> str_type:
940941

941942
@property
942943
def name(self) -> str_type:
943-
return f"period[{self.freq.freqstr}]"
944+
return f"period[{self._freqstr}]"
944945

945946
@property
946947
def na_value(self) -> NaTType:
@@ -955,12 +956,6 @@ def __eq__(self, other: Any) -> bool:
955956
def __ne__(self, other: Any) -> bool:
956957
return not self.__eq__(other)
957958

958-
def __setstate__(self, state) -> None:
959-
# for pickle compat. __getstate__ is defined in the
960-
# PandasExtensionDtype superclass and uses the public properties to
961-
# pickle -> need to set the settable private ones here (see GH26067)
962-
self._freq = state["freq"]
963-
964959
@classmethod
965960
def is_dtype(cls, dtype: object) -> bool:
966961
"""

0 commit comments

Comments
 (0)