1
- import types
2
1
from typing import Any , Callable , Dict , Optional , Tuple
3
2
4
3
import numpy as np
5
4
6
5
from pandas ._typing import Scalar
7
6
from pandas .compat ._optional import import_optional_dependency
8
7
8
+ from pandas .core .util .numba_ import (
9
+ check_kwargs_and_nopython ,
10
+ get_jit_arguments ,
11
+ jit_user_function ,
12
+ )
9
13
10
- def make_rolling_apply (
11
- func : Callable [..., Scalar ],
14
+
15
+ def generate_numba_apply_func (
12
16
args : Tuple ,
13
- nogil : bool ,
14
- parallel : bool ,
15
- nopython : bool ,
17
+ kwargs : Dict [ str , Any ] ,
18
+ func : Callable [..., Scalar ] ,
19
+ engine_kwargs : Optional [ Dict [ str , bool ]] ,
16
20
):
17
21
"""
18
- Creates a JITted rolling apply function with a JITted version of
19
- the user's function.
22
+ Generate a numba jitted apply function specified by values from engine_kwargs.
23
+
24
+ 1. jit the user's function
25
+ 2. Return a rolling apply function with the jitted function inline
26
+
27
+ Configurations specified in engine_kwargs apply to both the user's
28
+ function _AND_ the rolling apply function.
20
29
21
30
Parameters
22
31
----------
23
- func : function
24
- function to be applied to each window and will be JITed
25
32
args : tuple
26
33
*args to be passed into the function
27
- nogil : bool
28
- nogil parameter from engine_kwargs for numba.jit
29
- parallel : bool
30
- parallel parameter from engine_kwargs for numba.jit
31
- nopython : bool
32
- nopython parameter from engine_kwargs for numba.jit
34
+ kwargs : dict
35
+ **kwargs to be passed into the function
36
+ func : function
37
+ function to be applied to each window and will be JITed
38
+ engine_kwargs : dict
39
+ dictionary of arguments to be passed into numba.jit
33
40
34
41
Returns
35
42
-------
36
43
Numba function
37
44
"""
45
+ nopython , nogil , parallel = get_jit_arguments (engine_kwargs )
46
+
47
+ check_kwargs_and_nopython (kwargs , nopython )
48
+
49
+ numba_func = jit_user_function (func , nopython , nogil , parallel )
50
+
38
51
numba = import_optional_dependency ("numba" )
39
52
40
53
if parallel :
41
54
loop_range = numba .prange
42
55
else :
43
56
loop_range = range
44
57
45
- if isinstance (func , numba .targets .registry .CPUDispatcher ):
46
- # Don't jit a user passed jitted function
47
- numba_func = func
48
- else :
49
-
50
- @numba .generated_jit (nopython = nopython , nogil = nogil , parallel = parallel )
51
- def numba_func (window , * _args ):
52
- if getattr (np , func .__name__ , False ) is func or isinstance (
53
- func , types .BuiltinFunctionType
54
- ):
55
- jf = func
56
- else :
57
- jf = numba .jit (func , nopython = nopython , nogil = nogil )
58
-
59
- def impl (window , * _args ):
60
- return jf (window , * _args )
61
-
62
- return impl
63
-
64
58
@numba .jit (nopython = nopython , nogil = nogil , parallel = parallel )
65
59
def roll_apply (
66
60
values : np .ndarray , begin : np .ndarray , end : np .ndarray , minimum_periods : int ,
@@ -78,49 +72,3 @@ def roll_apply(
78
72
return result
79
73
80
74
return roll_apply
81
-
82
-
83
- def generate_numba_apply_func (
84
- args : Tuple ,
85
- kwargs : Dict [str , Any ],
86
- func : Callable [..., Scalar ],
87
- engine_kwargs : Optional [Dict [str , bool ]],
88
- ):
89
- """
90
- Generate a numba jitted apply function specified by values from engine_kwargs.
91
-
92
- 1. jit the user's function
93
- 2. Return a rolling apply function with the jitted function inline
94
-
95
- Configurations specified in engine_kwargs apply to both the user's
96
- function _AND_ the rolling apply function.
97
-
98
- Parameters
99
- ----------
100
- args : tuple
101
- *args to be passed into the function
102
- kwargs : dict
103
- **kwargs to be passed into the function
104
- func : function
105
- function to be applied to each window and will be JITed
106
- engine_kwargs : dict
107
- dictionary of arguments to be passed into numba.jit
108
-
109
- Returns
110
- -------
111
- Numba function
112
- """
113
- if engine_kwargs is None :
114
- engine_kwargs = {}
115
-
116
- nopython = engine_kwargs .get ("nopython" , True )
117
- nogil = engine_kwargs .get ("nogil" , False )
118
- parallel = engine_kwargs .get ("parallel" , False )
119
-
120
- if kwargs and nopython :
121
- raise ValueError (
122
- "numba does not support kwargs with nopython=True: "
123
- "https://github.com/numba/numba/issues/2916"
124
- )
125
-
126
- return make_rolling_apply (func , args , nogil , parallel , nopython )
0 commit comments