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:

  1. cdef iteration indices i , y integers: cdef int i,y (the cdefs go @ top of function they're in)
  2. cdef memoryview array access df.a/df['a'] through: cdef double[:] df_a_mv later df_a_mv = df.a (i've guessed @ type here, it's double)
  3. replace df.a memoryview (df_a_mv)
  4. 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

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -