Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove freq_to_period_freqstr from period.pyx
  • Loading branch information
natmokval committed Feb 13, 2024
commit daf707b0badc029cac32101f45db1b29bdbeb191
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/dtypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1
cdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso)
cdef bint is_supported_unit(NPY_DATETIMEUNIT reso)

cpdef freq_to_period_freqstr(freq_n, freq_name)
cdef dict c_OFFSET_TO_PERIOD_FREQSTR
cdef dict c_OFFSET_DEPR_FREQSTR
cdef dict c_REVERSE_OFFSET_DEPR_FREQSTR
Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/dtypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ OFFSET_TO_PERIOD_FREQSTR: dict[str, str]
def periods_per_day(reso: int = ...) -> int: ...
def periods_per_second(reso: int) -> int: ...
def abbrev_to_npy_unit(abbrev: str | None) -> int: ...
def freq_to_period_freqstr(freq_n: int, freq_name: str) -> str: ...

class PeriodDtypeBase:
_dtype_code: int # PeriodDtypeCode
Expand Down
9 changes: 0 additions & 9 deletions pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,6 @@ cdef dict c_REVERSE_OFFSET_DEPR_FREQSTR = {
v: k for k, v in c_OFFSET_DEPR_FREQSTR.items()
}

cpdef freq_to_period_freqstr(freq_n, freq_name):
if freq_n == 1:
freqstr = f"""{c_OFFSET_TO_PERIOD_FREQSTR.get(
freq_name, freq_name)}"""
else:
freqstr = f"""{freq_n}{c_OFFSET_TO_PERIOD_FREQSTR.get(
freq_name, freq_name)}"""
return freqstr

# Map deprecated resolution abbreviations to correct resolution abbreviations
cdef dict c_DEPR_ABBREVS = {
"A": "Y",
Expand Down
23 changes: 12 additions & 11 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ from libc.time cimport (
tm,
)

from pandas._libs.tslibs.dtypes cimport (
c_OFFSET_TO_PERIOD_FREQSTR,
freq_to_period_freqstr,
)
from pandas._libs.tslibs.dtypes cimport c_OFFSET_TO_PERIOD_FREQSTR

from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime

Expand Down Expand Up @@ -97,9 +94,6 @@ from pandas._libs.tslibs.dtypes cimport (
attrname_to_abbrevs,
freq_group_code_to_npy_unit,
)

from pandas._libs.tslibs.dtypes import freq_to_period_freqstr

from pandas._libs.tslibs.parsing cimport quarter_to_myear

from pandas._libs.tslibs.parsing import parse_datetime_string_with_reso
Expand Down Expand Up @@ -1554,7 +1548,7 @@ def extract_ordinals(ndarray values, freq) -> np.ndarray:
# if we don't raise here, we'll segfault later!
raise TypeError("extract_ordinals values must be object-dtype")

freqstr = freq_to_period_freqstr(freq.n, freq.name)
freqstr = PeriodDtypeBase(freq._period_dtype_code, freq.n)._freqstr

for i in range(n):
# Analogous to: p = values[i]
Expand Down Expand Up @@ -1722,8 +1716,15 @@ cdef class PeriodMixin:
condition = self.freq != other

if condition:
freqstr = freq_to_period_freqstr(self.freq.n, self.freq.name)
other_freqstr = freq_to_period_freqstr(other.n, other.name)
freqstr = PeriodDtypeBase(
self.freq._period_dtype_code, self.freq.n
)._freqstr
if hasattr(other, "_period_dtype_code"):
other_freqstr = PeriodDtypeBase(
other._period_dtype_code, other.n
)._freqstr
else:
other_freqstr = other.freqstr
Comment on lines -1725 to +1727
Copy link
Member

Choose a reason for hiding this comment

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

why do we need the if-else branch for other but not for self.freq?

Copy link
Contributor Author

@natmokval natmokval Feb 15, 2024

Choose a reason for hiding this comment

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

self.freq always has attribute "_period_dtype_code", bur other sometimes doesn't.
For example in many tests in pandas/tests/arithmetic/test_period.py( e.g. test_pi_add_sub_timedeltalike_freq_mismatch_monthly) other is equal pd.offsets.YearBegin(2)

msg = DIFFERENT_FREQ.format(
cls=type(self).__name__,
own_freq=freqstr,
Expand Down Expand Up @@ -2479,7 +2480,7 @@ cdef class _Period(PeriodMixin):
>>> pd.Period('2020-01', 'D').freqstr
'D'
"""
freqstr = freq_to_period_freqstr(self.freq.n, self.freq.name)
freqstr = PeriodDtypeBase(self.freq._period_dtype_code, self.freq.n)._freqstr
return freqstr

def __repr__(self) -> str:
Expand Down