Skip to content

Commit 901b1c7

Browse files
authored
REF: make ccalendar objects cimportable (pandas-dev#34106)
1 parent 95a9b8e commit 901b1c7

9 files changed

+42
-27
lines changed

pandas/_libs/tslibs/ccalendar.pxd

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil
1010
cpdef int32_t get_week_of_year(int year, int month, int day) nogil
1111
cpdef iso_calendar_t get_iso_calendar(int year, int month, int day) nogil
1212
cpdef int32_t get_day_of_year(int year, int month, int day) nogil
13+
14+
cdef int64_t DAY_NANOS
15+
cdef int64_t HOUR_NANOS
16+
cdef dict c_MONTH_NUMBERS

pandas/_libs/tslibs/ccalendar.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ MONTHS_FULL = ['', 'January', 'February', 'March', 'April', 'May', 'June',
4040
'July', 'August', 'September', 'October', 'November',
4141
'December']
4242
MONTH_NUMBERS = {name: num for num, name in enumerate(MONTHS)}
43+
cdef dict c_MONTH_NUMBERS = MONTH_NUMBERS
4344
MONTH_ALIASES = {(num + 1): name for num, name in enumerate(MONTHS)}
4445
MONTH_TO_CAL_NUM = {name: num + 1 for num, name in enumerate(MONTHS)}
4546

@@ -52,6 +53,9 @@ weekday_to_int = {int_to_weekday[key]: key for key in int_to_weekday}
5253
DAY_SECONDS = 86400
5354
HOUR_SECONDS = 3600
5455

56+
cdef int64_t DAY_NANOS = DAY_SECONDS * 1_000_000_000
57+
cdef int64_t HOUR_NANOS = HOUR_SECONDS * 1_000_000_000
58+
5559
# ----------------------------------------------------------------------
5660

5761

pandas/_libs/tslibs/fields.pyx

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ from numpy cimport ndarray, int64_t, int32_t, int8_t, uint32_t
1212
cnp.import_array()
1313

1414
from pandas._libs.tslibs.ccalendar import (
15-
get_locale_names, MONTHS_FULL, DAYS_FULL, DAY_SECONDS)
15+
get_locale_names, MONTHS_FULL, DAYS_FULL,
16+
)
1617
from pandas._libs.tslibs.ccalendar cimport (
18+
DAY_NANOS,
1719
get_days_in_month, is_leapyear, dayofweek, get_week_of_year,
1820
get_day_of_year, get_iso_calendar, iso_calendar_t)
1921
from pandas._libs.tslibs.np_datetime cimport (
@@ -38,7 +40,7 @@ def get_time_micros(const int64_t[:] dtindex):
3840
cdef:
3941
ndarray[int64_t] micros
4042

41-
micros = np.mod(dtindex, DAY_SECONDS * 1_000_000_000, dtype=np.int64)
43+
micros = np.mod(dtindex, DAY_NANOS, dtype=np.int64)
4244
micros //= 1000
4345
return micros
4446

pandas/_libs/tslibs/frequencies.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cnp.import_array()
55

66
from pandas._libs.tslibs.util cimport is_integer_object, is_offset_object
77

8-
from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
8+
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
99

1010
# ----------------------------------------------------------------------
1111
# Constants
@@ -458,8 +458,8 @@ cdef str _maybe_coerce_freq(code):
458458

459459

460460
cdef bint _quarter_months_conform(str source, str target):
461-
snum = MONTH_NUMBERS[source]
462-
tnum = MONTH_NUMBERS[target]
461+
snum = c_MONTH_NUMBERS[source]
462+
tnum = c_MONTH_NUMBERS[target]
463463
return snum % 3 == tnum % 3
464464

465465

pandas/_libs/tslibs/parsing.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ from dateutil.parser import parse as du_parse
3232

3333
from pandas._config import get_option
3434

35-
from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
35+
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
3636
from pandas._libs.tslibs.nattype import nat_strings, NaT
3737
from pandas._libs.tslibs.util cimport (
3838
is_array,
@@ -435,9 +435,9 @@ cdef inline object _parse_dateabbr_string(object date_string, object default,
435435
f'between 1 and 4: {date_string}')
436436

437437
if freq is not None:
438-
# hack attack, #1228
438+
# TODO: hack attack, #1228
439439
try:
440-
mnum = MONTH_NUMBERS[get_rule_month(freq)] + 1
440+
mnum = c_MONTH_NUMBERS[get_rule_month(freq)] + 1
441441
except (KeyError, ValueError):
442442
raise DateParseError(f'Unable to retrieve month '
443443
f'information from given '

pandas/_libs/tslibs/period.pyx

+11-6
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info
4444
from pandas._libs.tslibs.timedeltas import Timedelta
4545
from pandas._libs.tslibs.timedeltas cimport delta_to_nanoseconds
4646

47-
cimport pandas._libs.tslibs.ccalendar as ccalendar
48-
from pandas._libs.tslibs.ccalendar cimport dayofweek, get_day_of_year, is_leapyear
49-
from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
47+
from pandas._libs.tslibs.ccalendar cimport (
48+
dayofweek,
49+
get_day_of_year,
50+
is_leapyear,
51+
get_week_of_year,
52+
get_days_in_month,
53+
)
54+
from pandas._libs.tslibs.ccalendar cimport c_MONTH_NUMBERS
5055
from pandas._libs.tslibs.frequencies cimport (
5156
get_base_alias,
5257
get_freq_code,
@@ -1315,7 +1320,7 @@ cdef int pweek(int64_t ordinal, int freq):
13151320
cdef:
13161321
npy_datetimestruct dts
13171322
get_date_info(ordinal, freq, &dts)
1318-
return ccalendar.get_week_of_year(dts.year, dts.month, dts.day)
1323+
return get_week_of_year(dts.year, dts.month, dts.day)
13191324

13201325

13211326
cdef int phour(int64_t ordinal, int freq):
@@ -1343,7 +1348,7 @@ cdef int pdays_in_month(int64_t ordinal, int freq):
13431348
cdef:
13441349
npy_datetimestruct dts
13451350
get_date_info(ordinal, freq, &dts)
1346-
return ccalendar.get_days_in_month(dts.year, dts.month)
1351+
return get_days_in_month(dts.year, dts.month)
13471352

13481353

13491354
@cython.wraparound(False)
@@ -2486,7 +2491,7 @@ def quarter_to_myear(year: int, quarter: int, freq):
24862491
if quarter <= 0 or quarter > 4:
24872492
raise ValueError('Quarter must be 1 <= q <= 4')
24882493

2489-
mnum = MONTH_NUMBERS[get_rule_month(freq)] + 1
2494+
mnum = c_MONTH_NUMBERS[get_rule_month(freq)] + 1
24902495
month = (mnum + (quarter - 1) * 3) % 12 + 1
24912496
if month > mnum:
24922497
year -= 1

pandas/_libs/tslibs/timedeltas.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ from pandas._libs.tslibs.util cimport (
2323

2424
from pandas._libs.tslibs.base cimport ABCTimedelta, ABCTimestamp, is_tick_object
2525

26-
from pandas._libs.tslibs.ccalendar import DAY_SECONDS
26+
from pandas._libs.tslibs.ccalendar cimport DAY_NANOS
2727

2828
from pandas._libs.tslibs.np_datetime cimport (
2929
cmp_scalar, reverse_ops, td64_to_tdstruct, pandas_timedeltastruct)
@@ -277,10 +277,10 @@ cpdef inline object precision_from_unit(str unit):
277277
m = 1000000000 * 2629746
278278
p = 9
279279
elif unit == 'W':
280-
m = 1000000000 * DAY_SECONDS * 7
280+
m = DAY_NANOS * 7
281281
p = 9
282282
elif unit == 'D' or unit == 'd':
283-
m = 1000000000 * DAY_SECONDS
283+
m = DAY_NANOS
284284
p = 9
285285
elif unit == 'h':
286286
m = 1000000000 * 3600

pandas/_libs/tslibs/timestamps.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ from pandas._libs.tslibs.util cimport (
2727

2828
from pandas._libs.tslibs.base cimport ABCTimestamp, is_tick_object
2929

30-
cimport pandas._libs.tslibs.ccalendar as ccalendar
31-
from pandas._libs.tslibs.ccalendar import DAY_SECONDS
30+
from pandas._libs.tslibs cimport ccalendar
31+
3232
from pandas._libs.tslibs.conversion import normalize_i8_timestamps
3333
from pandas._libs.tslibs.conversion cimport (
3434
_TSObject, convert_to_tsobject,
@@ -1504,7 +1504,7 @@ default 'raise'
15041504
Normalize Timestamp to midnight, preserving tz information.
15051505
"""
15061506
if self.tz is None or is_utc(self.tz):
1507-
DAY_NS = DAY_SECONDS * 1_000_000_000
1507+
DAY_NS = ccalendar.DAY_NANOS
15081508
normalized_value = self.value - (self.value % DAY_NS)
15091509
return Timestamp(normalized_value).tz_localize(self.tz)
15101510
normalized_value = normalize_i8_timestamps(

pandas/_libs/tslibs/tzconversion.pyx

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cimport numpy as cnp
1616
from numpy cimport ndarray, int64_t, uint8_t, intp_t
1717
cnp.import_array()
1818

19-
from pandas._libs.tslibs.ccalendar import DAY_SECONDS, HOUR_SECONDS
19+
from pandas._libs.tslibs.ccalendar cimport DAY_NANOS, HOUR_NANOS
2020
from pandas._libs.tslibs.nattype cimport NPY_NAT
2121
from pandas._libs.tslibs.np_datetime cimport (
2222
npy_datetimestruct, dt64_to_dtstruct)
@@ -71,7 +71,7 @@ timedelta-like}
7171
int64_t *tdata
7272
int64_t v, left, right, val, v_left, v_right, new_local, remaining_mins
7373
int64_t first_delta
74-
int64_t HOURS_NS = HOUR_SECONDS * 1000000000, shift_delta = 0
74+
int64_t shift_delta = 0
7575
ndarray[int64_t] trans, result, result_a, result_b, dst_hours, delta
7676
ndarray trans_idx, grp, a_idx, b_idx, one_diff
7777
npy_datetimestruct dts
@@ -142,10 +142,10 @@ timedelta-like}
142142
result_b[:] = NPY_NAT
143143

144144
idx_shifted_left = (np.maximum(0, trans.searchsorted(
145-
vals - DAY_SECONDS * 1000000000, side='right') - 1)).astype(np.int64)
145+
vals - DAY_NANOS, side='right') - 1)).astype(np.int64)
146146

147147
idx_shifted_right = (np.maximum(0, trans.searchsorted(
148-
vals + DAY_SECONDS * 1000000000, side='right') - 1)).astype(np.int64)
148+
vals + DAY_NANOS, side='right') - 1)).astype(np.int64)
149149

150150
for i in range(n):
151151
val = vals[i]
@@ -240,18 +240,18 @@ timedelta-like}
240240
# Handle nonexistent times
241241
if shift_forward or shift_backward or shift_delta != 0:
242242
# Shift the nonexistent time to the closest existing time
243-
remaining_mins = val % HOURS_NS
243+
remaining_mins = val % HOUR_NANOS
244244
if shift_delta != 0:
245245
# Validate that we don't relocalize on another nonexistent
246246
# time
247-
if -1 < shift_delta + remaining_mins < HOURS_NS:
247+
if -1 < shift_delta + remaining_mins < HOUR_NANOS:
248248
raise ValueError(
249249
f"The provided timedelta will relocalize on a "
250250
f"nonexistent time: {nonexistent}"
251251
)
252252
new_local = val + shift_delta
253253
elif shift_forward:
254-
new_local = val + (HOURS_NS - remaining_mins)
254+
new_local = val + (HOUR_NANOS - remaining_mins)
255255
else:
256256
# Subtract 1 since the beginning hour is _inclusive_ of
257257
# nonexistent times

0 commit comments

Comments
 (0)