Skip to content

Commit cc1c406

Browse files
authored
CLN: libperiod (#31527)
1 parent 149c772 commit cc1c406

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

pandas/_libs/tslibs/period.pyx

+31-42
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,6 @@ cdef int DtoQ_yq(int64_t ordinal, asfreq_info *af_info, int *year) nogil:
473473
int quarter
474474

475475
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
476-
# TODO: Another version of this function used
477-
# date_info_from_days_and_time(&dts, unix_date, 0)
478-
# instead of pandas_datetime_to_datetimestruct; is one more performant?
479476
if af_info.to_end != 12:
480477
dts.month -= af_info.to_end
481478
if dts.month <= 0:
@@ -516,7 +513,7 @@ cdef int64_t asfreq_DTtoW(int64_t ordinal, asfreq_info *af_info) nogil:
516513
# Conversion _from_ BusinessDay Freq
517514

518515
cdef int64_t asfreq_BtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
519-
ordinal = ((ordinal + 3) // 5) * 7 + (ordinal + 3) % 5 -3
516+
ordinal = ((ordinal + 3) // 5) * 7 + (ordinal + 3) % 5 - 3
520517
return upsample_daytime(ordinal, af_info)
521518

522519

@@ -753,14 +750,7 @@ cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil:
753750
if fmonth == 0:
754751
fmonth = 12
755752

756-
mdiff = dts.month - fmonth
757-
# TODO: Aren't the next two conditions equivalent to
758-
# unconditional incrementing?
759-
if mdiff < 0:
760-
mdiff += 12
761-
if dts.month >= fmonth:
762-
mdiff += 12
763-
753+
mdiff = dts.month - fmonth + 12
764754
return (dts.year - 1970) * 4 + (mdiff - 1) // 3
765755

766756
elif freq == FR_MTH:
@@ -804,17 +794,16 @@ cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil:
804794
delta = (unix_date + 3) % 7 + 1
805795
# return the number of business days in full weeks plus the business
806796
# days in the last - possible partial - week
807-
if delta <= 5:
808-
return (5 * weeks) + delta - 4
809-
else:
810-
return (5 * weeks) + (5 + 1) - 4
797+
if delta > 6:
798+
# We have a Sunday, which rolls back to the previous Friday,
799+
# just like Saturday, so decrement delta by 1 to treat as saturday
800+
delta = 6
801+
return (5 * weeks) + delta - 4
811802

812803
elif freq_group == FR_WK:
813804
day_adj = freq - FR_WK
814805
return (unix_date + 3 - day_adj) // 7 + 1
815806

816-
# raise ValueError
817-
818807

819808
cdef void get_date_info(int64_t ordinal, int freq,
820809
npy_datetimestruct *dts) nogil:
@@ -983,7 +972,7 @@ cdef inline int month_to_quarter(int month) nogil:
983972

984973
@cython.wraparound(False)
985974
@cython.boundscheck(False)
986-
def dt64arr_to_periodarr(int64_t[:] dtarr, int freq, tz=None):
975+
def dt64arr_to_periodarr(const int64_t[:] dtarr, int freq, tz=None):
987976
"""
988977
Convert array of datetime64 values (passed in as 'i8' dtype) to a set of
989978
periods corresponding to desired frequency, per period convention.
@@ -1383,7 +1372,7 @@ cdef int pdays_in_month(int64_t ordinal, int freq):
13831372

13841373
@cython.wraparound(False)
13851374
@cython.boundscheck(False)
1386-
def get_period_field_arr(int code, int64_t[:] arr, int freq):
1375+
def get_period_field_arr(int code, const int64_t[:] arr, int freq):
13871376
cdef:
13881377
Py_ssize_t i, sz
13891378
int64_t[:] out
@@ -1496,7 +1485,7 @@ def extract_freq(ndarray[object] values):
14961485

14971486
@cython.wraparound(False)
14981487
@cython.boundscheck(False)
1499-
cdef int64_t[:] localize_dt64arr_to_period(int64_t[:] stamps,
1488+
cdef int64_t[:] localize_dt64arr_to_period(const int64_t[:] stamps,
15001489
int freq, object tz):
15011490
cdef:
15021491
Py_ssize_t n = len(stamps)
@@ -1584,7 +1573,7 @@ cdef class _Period:
15841573
return freq
15851574

15861575
@classmethod
1587-
def _from_ordinal(cls, ordinal, freq):
1576+
def _from_ordinal(cls, ordinal: int, freq) -> "Period":
15881577
"""
15891578
Fast creation from an ordinal and freq that are already validated!
15901579
"""
@@ -1704,7 +1693,7 @@ cdef class _Period:
17041693
else:
17051694
return NotImplemented
17061695

1707-
def asfreq(self, freq, how='E'):
1696+
def asfreq(self, freq, how='E') -> "Period":
17081697
"""
17091698
Convert Period to desired frequency, at the start or end of the interval.
17101699

@@ -1735,7 +1724,7 @@ cdef class _Period:
17351724
return Period(ordinal=ordinal, freq=freq)
17361725

17371726
@property
1738-
def start_time(self):
1727+
def start_time(self) -> Timestamp:
17391728
"""
17401729
Get the Timestamp for the start of the period.
17411730

@@ -1765,13 +1754,13 @@ cdef class _Period:
17651754
return self.to_timestamp(how='S')
17661755

17671756
@property
1768-
def end_time(self):
1757+
def end_time(self) -> Timestamp:
17691758
# freq.n can't be negative or 0
17701759
# ordinal = (self + self.freq.n).start_time.value - 1
17711760
ordinal = (self + self.freq).start_time.value - 1
17721761
return Timestamp(ordinal)
17731762

1774-
def to_timestamp(self, freq=None, how='start', tz=None):
1763+
def to_timestamp(self, freq=None, how='start', tz=None) -> Timestamp:
17751764
"""
17761765
Return the Timestamp representation of the Period.
17771766

@@ -1811,17 +1800,17 @@ cdef class _Period:
18111800
return Timestamp(dt64, tz=tz)
18121801

18131802
@property
1814-
def year(self):
1803+
def year(self) -> int:
18151804
base, mult = get_freq_code(self.freq)
18161805
return pyear(self.ordinal, base)
18171806

18181807
@property
1819-
def month(self):
1808+
def month(self) -> int:
18201809
base, mult = get_freq_code(self.freq)
18211810
return pmonth(self.ordinal, base)
18221811

18231812
@property
1824-
def day(self):
1813+
def day(self) -> int:
18251814
"""
18261815
Get day of the month that a Period falls on.
18271816

@@ -1844,7 +1833,7 @@ cdef class _Period:
18441833
return pday(self.ordinal, base)
18451834

18461835
@property
1847-
def hour(self):
1836+
def hour(self) -> int:
18481837
"""
18491838
Get the hour of the day component of the Period.
18501839

@@ -1874,7 +1863,7 @@ cdef class _Period:
18741863
return phour(self.ordinal, base)
18751864

18761865
@property
1877-
def minute(self):
1866+
def minute(self) -> int:
18781867
"""
18791868
Get minute of the hour component of the Period.
18801869

@@ -1898,7 +1887,7 @@ cdef class _Period:
18981887
return pminute(self.ordinal, base)
18991888

19001889
@property
1901-
def second(self):
1890+
def second(self) -> int:
19021891
"""
19031892
Get the second component of the Period.
19041893

@@ -1922,12 +1911,12 @@ cdef class _Period:
19221911
return psecond(self.ordinal, base)
19231912

19241913
@property
1925-
def weekofyear(self):
1914+
def weekofyear(self) -> int:
19261915
base, mult = get_freq_code(self.freq)
19271916
return pweek(self.ordinal, base)
19281917

19291918
@property
1930-
def week(self):
1919+
def week(self) -> int:
19311920
"""
19321921
Get the week of the year on the given Period.
19331922

@@ -1957,7 +1946,7 @@ cdef class _Period:
19571946
return self.weekofyear
19581947

19591948
@property
1960-
def dayofweek(self):
1949+
def dayofweek(self) -> int:
19611950
"""
19621951
Day of the week the period lies in, with Monday=0 and Sunday=6.
19631952

@@ -2008,7 +1997,7 @@ cdef class _Period:
20081997
return pweekday(self.ordinal, base)
20091998

20101999
@property
2011-
def weekday(self):
2000+
def weekday(self) -> int:
20122001
"""
20132002
Day of the week the period lies in, with Monday=0 and Sunday=6.
20142003

@@ -2061,7 +2050,7 @@ cdef class _Period:
20612050
return self.dayofweek
20622051

20632052
@property
2064-
def dayofyear(self):
2053+
def dayofyear(self) -> int:
20652054
"""
20662055
Return the day of the year.
20672056

@@ -2096,12 +2085,12 @@ cdef class _Period:
20962085
return pday_of_year(self.ordinal, base)
20972086

20982087
@property
2099-
def quarter(self):
2088+
def quarter(self) -> int:
21002089
base, mult = get_freq_code(self.freq)
21012090
return pquarter(self.ordinal, base)
21022091

21032092
@property
2104-
def qyear(self):
2093+
def qyear(self) -> int:
21052094
"""
21062095
Fiscal year the Period lies in according to its starting-quarter.
21072096

@@ -2145,7 +2134,7 @@ cdef class _Period:
21452134
return pqyear(self.ordinal, base)
21462135

21472136
@property
2148-
def days_in_month(self):
2137+
def days_in_month(self) -> int:
21492138
"""
21502139
Get the total number of days in the month that this period falls on.
21512140

@@ -2179,7 +2168,7 @@ cdef class _Period:
21792168
return pdays_in_month(self.ordinal, base)
21802169

21812170
@property
2182-
def daysinmonth(self):
2171+
def daysinmonth(self) -> int:
21832172
"""
21842173
Get the total number of days of the month that the Period falls in.
21852174

@@ -2209,7 +2198,7 @@ cdef class _Period:
22092198
return Period(datetime.now(), freq=freq)
22102199

22112200
@property
2212-
def freqstr(self):
2201+
def freqstr(self) -> str:
22132202
return self.freq.freqstr
22142203

22152204
def __repr__(self) -> str:

pandas/tests/scalar/period/test_period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ def test_properties_secondly(self):
925925

926926
class TestPeriodField:
927927
def test_get_period_field_array_raises_on_out_of_range(self):
928-
msg = "Buffer dtype mismatch, expected 'int64_t' but got 'double'"
928+
msg = "Buffer dtype mismatch, expected 'const int64_t' but got 'double'"
929929
with pytest.raises(ValueError, match=msg):
930930
libperiod.get_period_field_arr(-1, np.empty(1), 0)
931931

0 commit comments

Comments
 (0)