# Buggy code
def mean_parallel_impl(return_type, arg):
    # can't reuse sum since output type is different
    zero = return_type(0)

    if arg.ndim == 1:

        def mean_1(in_arr):
            numba.parfor.init_prange()
            val = zero
            for i in numba.parfor.internal_prange(len(in_arr)):
                val += in_arr[i]
            return val / len(in_arr)
    else:

        def mean_1(in_arr):
            numba.parfor.init_prange()
            val = zero
            for i in numba.pndindex(in_arr.shape):
                val += in_arr[i]
            return val / in_arr.size

    return mean_1


# Traceback (IndexError)
Traceback (most recent call last):

File "<ipython-input-101-c2c55c205bd8>", line 1, in <module>
runfile('C:/Users/rpham/Desktop/FIN_Library/test.py', wdir='C:/Users/rpham/Desktop/FIN_Library')

File "C:\Users\rpham\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\Users\rpham\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/rpham/Desktop/FIN_Library/test.py", line 19, in <module>
result = fama_macbeth_numba(df,'period','ret',['exmkt','smb','hml'],intercept=True,parallel=True)

File "C:\Users\rpham\Desktop\FIN_Library\finance_byu\fama_macbeth.py", line 202, in fama_macbeth_numba
gammas = _ols_numba_parallel(d,ti,np.array(yvari),np.array(xvari))

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\dispatcher.py", line 370, in _compile_for_args
raise e

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\dispatcher.py", line 327, in _compile_for_args
return self.compile(tuple(argtypes))

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\dispatcher.py", line 659, in compile
cres = self._compiler.compile(args, return_type)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\dispatcher.py", line 83, in compile
pipeline_class=self.pipeline_class)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 955, in compile_extra
return pipeline.compile_extra(func)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 377, in compile_extra
return self._compile_bytecode()

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 886, in _compile_bytecode
return self._compile_core()

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 873, in _compile_core
res = pm.run(self.status)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 254, in run
raise patched_exception

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 245, in run
stage()

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\compiler.py", line 565, in stage_parfor_pass
parfor_pass.run()

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\parfor.py", line 1448, in run
self.array_analysis.run(self.func_ir.blocks)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\array_analysis.py", line 916, in run
pre, post = self._analyze_inst(label, scope, equiv_set, inst)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\array_analysis.py", line 987, in _analyze_inst
equiv_set.insert_equiv(lhs, shape)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\array_analysis.py", line 445, in insert_equiv
obj_names = [self._get_names(x) for x in objs]

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\array_analysis.py", line 445, in <listcomp>
obj_names = [self._get_names(x) for x in objs]

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\array_analysis.py", line 363, in _get_names
return tuple(self._get_names(x)[0] for x in obj)

File "C:\Users\rpham\Anaconda3\lib\site-packages\numba\array_analysis.py", line 363, in <genexpr>
return tuple(self._get_names(x)[0] for x in obj)

IndexError: Failed in nopython mode pipeline (step: convert to parfors)
tuple index out of range

# Fixed code
def mean_parallel_impl(return_type, arg):
    # can't reuse sum since output type is different
    zero = return_type(0)

    if arg.ndim == 0:

        def mean_1(in_arr):
            return in_arr[()]
    elif arg.ndim == 1:

        def mean_1(in_arr):
            numba.parfor.init_prange()
            val = zero
            for i in numba.parfor.internal_prange(len(in_arr)):
                val += in_arr[i]
            return val / len(in_arr)
    else:

        def mean_1(in_arr):
            numba.parfor.init_prange()
            val = zero
            for i in numba.pndindex(in_arr.shape):
                val += in_arr[i]
            return val / in_arr.size

    return mean_1