Ledger Yu Ledger Yu - 1 month ago 5
Python Question

Plot Cumulative Returns of a Pandas DataFrame

I have a pandas time-series dataframe:

q1 q2 q3 q4 q5
2001-09-30 0.211770 0.085683 0.050699 0.037150 0.042206
2001-10-31 0.203492 0.107347 0.075474 0.063460 0.052081
2001-11-30 0.089562 0.064950 0.055432 0.058544 0.052215
2001-12-31 -0.078566 -0.017314 -0.015337 0.003871 0.013463
2002-01-31 -0.151178 -0.024362 -0.012938 -0.005622 -0.010209


What's the most elegant way of computing cumulative returns of each bucket and then plot a line chart?

For example, cumulative return of 'q1' at 2001-11-30 is calculated as:

(1+0.211770) x (1+0.203492) x (1+0.089562) - 1 = 0.588969


And the final line chart should look something like this (I don't really care about the aesthetics of the chart, e.g., font, date format, legend location, etc.):

enter image description here

Answer

add 1, then use cumprod(), subtract 1 and then plot().

((df + 1).cumprod() - 1).plot()
Comments