python - Cythonise a pandas loop -
can show me how convert loop cython improve performance. need create static types using cdef performance else required:
if have dataframe df column 'a'.
in range(0, len(df.a)-1): if (i < len(df.a)-1): y= + 1 while ((np.abs(df.a[y]- df.a[i]) <= 0.015) & (y < len(df.a)-1)): y = y + 1 if df[a][y] - df[a][i] >= 0.015: df['dir_y'][i] = 1 #print(1) else: df['dir_y'][i] = -1 #print(-1)
i pretty sure 'cythonise' not word seemed appropriate.
without trying comment on whether write better in pandas without using cython (i don't know, it's worth trying), steps you'd need are:
cdef
iteration indicesi
,y
integers:cdef int i,y
(the cdefs go @ top of function they're in)cdef
memoryview array accessdf.a
/df['a']
through:cdef double[:] df_a_mv
laterdf_a_mv = df.a
(i've guessed @ type here, it's double)- replace
df.a
memoryview (df_a_mv
) - compile in cython (see http://docs.cython.org/src/reference/compilation.html)
you want run cython -a <your_file>.pyx
see has done - generates html file , lines highlighted in yellow unoptimised bits.
i wouldn't worry df['dir_y'][i]
- gets done infrequently , can't speed them much.
as final small point: if (i < len(df.a)-1):
unnecessary - it's guaranteed surrounding for
loop.
Comments
Post a Comment