Skip to content

Commit 3b9d1f6

Browse files
authored
TYP: tighter typing in _maybe_convert_freq, _from_ordinal (#56389)
1 parent aed4df1 commit 3b9d1f6

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

pandas/_libs/tslibs/period.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Period(PeriodMixin):
8787
@classmethod
8888
def _maybe_convert_freq(cls, freq) -> BaseOffset: ...
8989
@classmethod
90-
def _from_ordinal(cls, ordinal: int, freq) -> Period: ...
90+
def _from_ordinal(cls, ordinal: int, freq: BaseOffset) -> Period: ...
9191
@classmethod
9292
def now(cls, freq: Frequency) -> Period: ...
9393
def strftime(self, fmt: str | None) -> str: ...

pandas/_libs/tslibs/period.pyx

+8-14
Original file line numberDiff line numberDiff line change
@@ -1756,21 +1756,12 @@ cdef class _Period(PeriodMixin):
17561756
@classmethod
17571757
def _maybe_convert_freq(cls, object freq) -> BaseOffset:
17581758
"""
1759-
Internally we allow integer and tuple representations (for now) that
1760-
are not recognized by to_offset, so we convert them here. Also, a
1761-
Period's freq attribute must have `freq.n > 0`, which we check for here.
1759+
A Period's freq attribute must have `freq.n > 0`, which we check for here.
17621760

17631761
Returns
17641762
-------
17651763
DateOffset
17661764
"""
1767-
if isinstance(freq, int):
1768-
# We already have a dtype code
1769-
dtype = PeriodDtypeBase(freq, 1)
1770-
freq = dtype._freqstr
1771-
elif isinstance(freq, PeriodDtypeBase):
1772-
freq = freq._freqstr
1773-
17741765
freq = to_offset(freq, is_period=True)
17751766

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

17821773
@classmethod
1783-
def _from_ordinal(cls, ordinal: int64_t, freq) -> "Period":
1774+
def _from_ordinal(cls, ordinal: int64_t, freq: BaseOffset) -> "Period":
17841775
"""
17851776
Fast creation from an ordinal and freq that are already validated!
17861777
"""
@@ -1988,8 +1979,10 @@ cdef class _Period(PeriodMixin):
19881979
return endpoint - np.timedelta64(1, "ns")
19891980

19901981
if freq is None:
1991-
freq = self._dtype._get_to_timestamp_base()
1992-
base = freq
1982+
freq_code = self._dtype._get_to_timestamp_base()
1983+
dtype = PeriodDtypeBase(freq_code, 1)
1984+
freq = dtype._freqstr
1985+
base = freq_code
19931986
else:
19941987
freq = self._maybe_convert_freq(freq)
19951988
base = freq._period_dtype_code
@@ -2836,7 +2829,8 @@ class Period(_Period):
28362829
FutureWarning,
28372830
stacklevel=find_stack_level(),
28382831
)
2839-
2832+
if ordinal == NPY_NAT:
2833+
return NaT
28402834
return cls._from_ordinal(ordinal, freq)
28412835

28422836

pandas/core/arrays/period.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
)
3636
from pandas._libs.tslibs.dtypes import (
3737
FreqGroup,
38+
PeriodDtypeBase,
3839
freq_to_period_freqstr,
3940
)
4041
from pandas._libs.tslibs.fields import isleapyear_arr
@@ -652,8 +653,10 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
652653
return (self + self.freq).to_timestamp(how="start") - adjust
653654

654655
if freq is None:
655-
freq = self._dtype._get_to_timestamp_base()
656-
base = freq
656+
freq_code = self._dtype._get_to_timestamp_base()
657+
dtype = PeriodDtypeBase(freq_code, 1)
658+
freq = dtype._freqstr
659+
base = freq_code
657660
else:
658661
freq = Period._maybe_convert_freq(freq)
659662
base = freq._period_dtype_code

pandas/tests/scalar/period/test_period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def test_parse_week_str_roundstrip(self):
418418

419419
def test_period_from_ordinal(self):
420420
p = Period("2011-01", freq="M")
421-
res = Period._from_ordinal(p.ordinal, freq="M")
421+
res = Period._from_ordinal(p.ordinal, freq=p.freq)
422422
assert p == res
423423
assert isinstance(res, Period)
424424

0 commit comments

Comments
 (0)