Skip to content

TYP: tighter typing in _maybe_convert_freq, _from_ordinal #56389

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 1 commit into from
Dec 8, 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
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Period(PeriodMixin):
@classmethod
def _maybe_convert_freq(cls, freq) -> BaseOffset: ...
@classmethod
def _from_ordinal(cls, ordinal: int, freq) -> Period: ...
def _from_ordinal(cls, ordinal: int, freq: BaseOffset) -> Period: ...
@classmethod
def now(cls, freq: Frequency) -> Period: ...
def strftime(self, fmt: str | None) -> str: ...
Expand Down
22 changes: 8 additions & 14 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1756,21 +1756,12 @@ cdef class _Period(PeriodMixin):
@classmethod
def _maybe_convert_freq(cls, object freq) -> BaseOffset:
"""
Internally we allow integer and tuple representations (for now) that
are not recognized by to_offset, so we convert them here. Also, a
Period's freq attribute must have `freq.n > 0`, which we check for here.
A Period's freq attribute must have `freq.n > 0`, which we check for here.

Returns
-------
DateOffset
"""
if isinstance(freq, int):
# We already have a dtype code
dtype = PeriodDtypeBase(freq, 1)
freq = dtype._freqstr
elif isinstance(freq, PeriodDtypeBase):
freq = freq._freqstr

freq = to_offset(freq, is_period=True)

if freq.n <= 0:
Expand All @@ -1780,7 +1771,7 @@ cdef class _Period(PeriodMixin):
return freq

@classmethod
def _from_ordinal(cls, ordinal: int64_t, freq) -> "Period":
def _from_ordinal(cls, ordinal: int64_t, freq: BaseOffset) -> "Period":
"""
Fast creation from an ordinal and freq that are already validated!
"""
Expand Down Expand Up @@ -1988,8 +1979,10 @@ cdef class _Period(PeriodMixin):
return endpoint - np.timedelta64(1, "ns")

if freq is None:
freq = self._dtype._get_to_timestamp_base()
base = freq
freq_code = self._dtype._get_to_timestamp_base()
dtype = PeriodDtypeBase(freq_code, 1)
freq = dtype._freqstr
base = freq_code
else:
freq = self._maybe_convert_freq(freq)
base = freq._period_dtype_code
Expand Down Expand Up @@ -2836,7 +2829,8 @@ class Period(_Period):
FutureWarning,
stacklevel=find_stack_level(),
)

if ordinal == NPY_NAT:
return NaT
return cls._from_ordinal(ordinal, freq)


Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
)
from pandas._libs.tslibs.dtypes import (
FreqGroup,
PeriodDtypeBase,
freq_to_period_freqstr,
)
from pandas._libs.tslibs.fields import isleapyear_arr
Expand Down Expand Up @@ -652,8 +653,10 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
return (self + self.freq).to_timestamp(how="start") - adjust

if freq is None:
freq = self._dtype._get_to_timestamp_base()
base = freq
freq_code = self._dtype._get_to_timestamp_base()
dtype = PeriodDtypeBase(freq_code, 1)
freq = dtype._freqstr
base = freq_code
else:
freq = Period._maybe_convert_freq(freq)
base = freq._period_dtype_code
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def test_parse_week_str_roundstrip(self):

def test_period_from_ordinal(self):
p = Period("2011-01", freq="M")
res = Period._from_ordinal(p.ordinal, freq="M")
res = Period._from_ordinal(p.ordinal, freq=p.freq)
assert p == res
assert isinstance(res, Period)

Expand Down