@@ -67,6 +67,18 @@ class RoundTo(enum.Enum):
67
67
NEAREST_HALF_MINUS_INFTY = 4
68
68
69
69
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
+
70
82
def round_nsint64 (values , mode: RoundTo , freq ):
71
83
"""
72
84
Applies rounding mode at given frequency
@@ -88,17 +100,17 @@ def round_nsint64(values, mode: RoundTo, freq):
88
100
unit = to_offset(freq).nanos
89
101
90
102
if mode is RoundTo.MINUS_INFTY:
91
- return values - (values % unit)
103
+ return _floor_int64 (values, unit)
92
104
elif mode is RoundTo.PLUS_INFTY:
93
- return values + ( - values % unit)
105
+ return _ceil_int64( values, unit)
94
106
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 )
96
108
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 )
98
110
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
100
112
if unit % 2 :
101
- return round_nsint64 (values, RoundTo.NEAREST_HALF_MINUS_INFTY, freq )
113
+ return _rounddown_int64 (values, unit )
102
114
d, r = np.divmod(values, unit)
103
115
mask = np.logical_or(
104
116
r > (unit // 2 ),
0 commit comments