antonio_zeus
antonio_zeus

Reputation: 497

python pandas portfolio return

I've got a dataframe with market data and one column dedicated to daily returns. I'm having a hard time creating a portfolio to start at $100,000.00 in value, and compute its cumulative return through the life of the data series.

Ideally, I'd like to compute the 'portfolio' column using pandas but I'm having trouble doing so. See below target output. Thank you.

index    date      index  return  portfolio
0        19900101  2000   Nan     100000.00
1        19900102  2002   0.001   100100.00
2        19900103  2020   0.00899 100999.90 
3        19900104  2001  -0.00941 100049.49

Upvotes: 1

Views: 1978

Answers (2)

Alexander
Alexander

Reputation: 109716

starting_value = 100000
df = df.assign(portfolio=(1 + df['return'].fillna(0)).cumprod().mul(starting_value))
>>> df
   index      date  index.1   return     portfolio
0      0  19900101     2000      NaN  100000.00000
1      1  19900102     2002  0.00100  100100.00000
2      2  19900103     2020  0.00899  100999.89900
3      3  19900104     2001 -0.00941  100049.48995

To visualize what is happening, cumprod is calculating compounded returns, e.g. cum_r3 = (1 + r1) * (1 + r2) * (1 + r3).

>>> (1 + df['return'].fillna(0)).cumprod()
0    1.000000
1    1.001000
2    1.009999
3    1.000495
Name: return, dtype: float64

Upvotes: 3

BENY
BENY

Reputation: 323366

By using cumprod

df['P']=df['return'].add(1).fillna(1).cumprod()*100000
df
Out[843]: 
   index      date  index.1   return  portfolio             P
0      0  19900101     2000      NaN  100000.00  100000.00000
1      1  19900102     2002  0.00100  100100.00  100100.00000
2      2  19900103     2020  0.00899  100999.90  100999.89900
3      3  19900104     2001 -0.00941  100049.49  100049.48995

Some adjustments:

df=df.replace('Nan',np.nan)
df['return']=pd.to_numeric(df['return'])

Upvotes: 4

Related Questions