Skip to content

Commit 0486ff1

Browse files
committed
avoid recursive calls with cdef inline functions
1 parent 2c78964 commit 0486ff1

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

pandas/_libs/tslibs/timestamps.pyx

+18-6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ class RoundTo(enum.Enum):
6767
NEAREST_HALF_MINUS_INFTY = 4
6868

6969

70+
cdef inline _floor_int64(v, u):
71+
return v - np.remainder(v, u)
72+
73+
cdef inline _ceil_int64(v, u):
74+
return v + np.remainder(-v, u)
75+
76+
cdef inline _rounddown_int64(v, u):
77+
return _ceil_int64(v - u//2, u)
78+
79+
cdef inline _roundup_int64(v, u):
80+
return _floor_int64(v + u//2, u)
81+
7082
def round_nsint64(values, mode: RoundTo, freq):
7183
"""
7284
Applies rounding mode at given frequency
@@ -88,17 +100,17 @@ def round_nsint64(values, mode: RoundTo, freq):
88100
unit = to_offset(freq).nanos
89101

90102
if mode is RoundTo.MINUS_INFTY:
91-
return values - (values % unit)
103+
return _floor_int64(values, unit)
92104
elif mode is RoundTo.PLUS_INFTY:
93-
return values + (-values % unit)
105+
return _ceil_int64(values, unit)
94106
elif mode is RoundTo.NEAREST_HALF_MINUS_INFTY:
95-
return round_nsint64(values - unit//2, RoundTo.PLUS_INFTY, freq)
107+
return _rounddown_int64(values, unit)
96108
elif mode is RoundTo.NEAREST_HALF_PLUS_INFTY:
97-
return round_nsint64(values + unit//2, RoundTo.MINUS_INFTY, freq)
109+
return _roundup_int64(values, unit)
98110
elif mode is RoundTo.NEAREST_HALF_EVEN:
99-
# for odd unit there is n need of a tie break
111+
# for odd unit there is no need of a tie break
100112
if unit % 2:
101-
return round_nsint64(values, RoundTo.NEAREST_HALF_MINUS_INFTY, freq)
113+
return _rounddown_int64(values, unit)
102114
d, r = np.divmod(values, unit)
103115
mask = np.logical_or(
104116
r > (unit // 2),

0 commit comments

Comments
 (0)