0

I am failrly new in Python. I wrote a script and I am surprised of the time it take to go through a particular loop compare to the rest of my code.

Can someone tell me what is inefficient in the code I wrote and maybe how to improve the speed ?

Here is the loop in question : (BT_Histos and Histos_Last_Rebal are dataframes with dates in index and columns of floats. Portfolio and Portfolio_Last_Rebal are dataframes same index as the 2 previous one that i am filling through the loop. weights is just a list)

Udl_Perf=BT_Histos/Histos_Last_Rebal-1

for i in range(1,len(BT_Histos.index)):
    """tricky because isin doesn't work with timestamp"""
    test_date=pd.Series(Portfolio.index[i-1])

    if test_date.isin(Rebalancing_Dates)[0]:
        Portfolio_Last_Rebal.loc[Portfolio_Last_Rebal.index[i],'PortSeries']=Portfolio.loc[Portfolio.index[i-1],'PortSeries']
    else:
        Portfolio_Last_Rebal.loc[Portfolio_Last_Rebal.index[i],'PortSeries']=Portfolio_Last_Rebal.loc[Portfolio_Last_Rebal.index[i-1],'PortSeries']

    Portfolio.loc[Portfolio.index[i],'PortSeries']=Portfolio_Last_Rebal.loc[Portfolio_Last_Rebal.index[i],'PortSeries']*(1+sum(Udl_Perf.iloc[i]*weights))

Thanks!

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Fred Dujardin
  • 127
  • 1
  • 1
  • 8
  • Welcome to SO. Please supply a **[mcve]**. See, in particular, [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – jpp Aug 23 '18 at 09:53

1 Answers1

0

If you really want it to be fast then first implement it in while loop.

Second the length variable which you will use, define the type in advance using Mypy library, in this you need Python 3.5+ version installed.

Also if every iteration is unique then you can use multithreading using threading library. Get eg in this git repo

Devendra Bhat
  • 1,149
  • 2
  • 14
  • 19