diff --git a/pandas/_libs/tslibs/period.pyi b/pandas/_libs/tslibs/period.pyi index df6ce675b07fc..22f3bdbe668de 100644 --- a/pandas/_libs/tslibs/period.pyi +++ b/pandas/_libs/tslibs/period.pyi @@ -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: ... diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 6b105f5974f9b..eeaf472c23b60 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -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: @@ -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! """ @@ -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 @@ -2836,7 +2829,8 @@ class Period(_Period): FutureWarning, stacklevel=find_stack_level(), ) - + if ordinal == NPY_NAT: + return NaT return cls._from_ordinal(ordinal, freq) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 5f267a9f816e7..1ff3896eea798 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -35,6 +35,7 @@ ) from pandas._libs.tslibs.dtypes import ( FreqGroup, + PeriodDtypeBase, freq_to_period_freqstr, ) from pandas._libs.tslibs.fields import isleapyear_arr @@ -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 diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 3e91264fdb3b1..06d82ccd72b8c 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -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)