I want calculate RSI indicator value for multiple column in Pandas DataFrame. I am looking for a method to avoid loop, here is the code I am using:

rsi_calculations = pd.DataFrame()
for column in rsi_trans.columns:
    rsi = ta.RSI(rsi_trans[column].values, timeperiod=30)
    rsi_calculations[column] = rsi

In the above code I am calculating RSI value and appending it to pandas data frame, but its taking more time.

Is there a more efficient way to do this?


I've imported TA-Lib as ta and used its RSI function to calculate RSI value.

  • \$\begingroup\$ yes you need to vectorise your ta.RSI function. You haven't said how it is implemented so either; you have to vectorise its original implementation, investigate using numpy.vectorize, or potentially explore using numba if it is compatible \$\endgroup\$ – Attack68 Mar 9 '19 at 11:03
  • \$\begingroup\$ I've added in my question where ta.RSI comes from. It's from a module TA-Lib. \$\endgroup\$ – Furqan Hashim Mar 9 '19 at 11:06
  • \$\begingroup\$ @Attack68 I think main goal of numpy.vectorize is to hide a for loop rather than enhancing performance. \$\endgroup\$ – Furqan Hashim Mar 9 '19 at 11:46
  • \$\begingroup\$ Yes I saw that myself just now actually. Perhaps the only way is to write the function yourself with vectorised numpy, from what I recall rsi is fairly straightforward calculation. Plus might be able to use numba for parallelization or other enhancements. \$\endgroup\$ – Attack68 Mar 9 '19 at 11:52
  • 1
    \$\begingroup\$ You might see speed ups if you only do array operations inside the loop and remove the data frame population to the end and do the whole array with one data frame creation - this might be an overhead but I am only speculating \$\endgroup\$ – Attack68 Mar 9 '19 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.