Skip to content

REF: use dtype.freq less in PeriodDtype #52585

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 2 commits into from
Apr 11, 2023
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
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ cdef class PeriodDtypeBase:
@property
def _freqstr(self) -> str:
# Will be passed to to_offset in Period._maybe_convert_freq
return _reverse_period_code_map.get(self._dtype_code)
out = _reverse_period_code_map.get(self._dtype_code)
if self._n == 1:
return out
return str(self._n) + out

cpdef int _get_to_timestamp_base(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,8 @@ cdef class _Period(PeriodMixin):
# We already have a dtype code
dtype = PeriodDtypeBase(freq, 1)
freq = dtype._freqstr
elif isinstance(freq, PeriodDtypeBase):
freq = freq._freqstr

freq = to_offset(freq)

Expand Down Expand Up @@ -2372,7 +2374,7 @@ cdef class _Period(PeriodMixin):
"""
Return a string representation of the frequency.
"""
return self.freq.freqstr
return self._dtype._freqstr

def __repr__(self) -> str:
base = self._dtype._dtype_code
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype):
_match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
__hash__ = PeriodDtypeBase.__hash__
_freq: BaseOffset

def __new__(cls, freq):
"""
Expand All @@ -886,7 +887,7 @@ def __new__(cls, freq):
return u

def __reduce__(self):
return type(self), (self.freq,)
return type(self), (self.name,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this break pickle compatibility?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. when it loads this gets passed to the constructor which converts the string


@property
def freq(self):
Expand Down Expand Up @@ -940,7 +941,7 @@ def __str__(self) -> str_type:

@property
def name(self) -> str_type:
return f"period[{self.freq.freqstr}]"
return f"period[{self._freqstr}]"

@property
def na_value(self) -> NaTType:
Expand All @@ -955,12 +956,6 @@ def __eq__(self, other: Any) -> bool:
def __ne__(self, other: Any) -> bool:
return not self.__eq__(other)

def __setstate__(self, state) -> None:
# for pickle compat. __getstate__ is defined in the
# PandasExtensionDtype superclass and uses the public properties to
# pickle -> need to set the settable private ones here (see GH26067)
self._freq = state["freq"]

@classmethod
def is_dtype(cls, dtype: object) -> bool:
"""
Expand Down