0

I have a bunch of Dataframes with datetime index and returns. I have to create a unique dataframe that sums all the returns of the dataframes by index.

DATAFRAME A
Date
12-12-2004       5
13-12-2004       7

DATAFRAME B
Date
12-12-2004       6
13-12-2004       -3

DATAFRAME A+B
Date
12-12-2004       11
13-12-2004       4

This is basically the concept. So, entering the code:

total_daily_returns = pd.DataFrame()

for pair in range(0, 10):
    returns_pair = pd.DataFrame()
    returns_pair['cum_daily_return'] = signals['column1']
    returns_pair['cumulative_returns'] = returns_pair['cum_daily_return'].cumsum()

    total_daily_returns = total_daily_returns + (
        returns_pair.cumulative_returns)

    print(total_daily_returns)

I expected it would have worked, instead I get:

    Empty DataFrame
Columns: [2004-06-28 00:00:00, 2004-06-29 00:00:00, 2004-06-30 00:00:00, 2004-07-01 00:00:00, 2004-07-02 00:00:00, 2004-07-05 00:00:00, 2004-07-06 00:00:00, 2004-07-07 00:00:00, 2004-07-08 00:00:00, 2004-07-09 00:00:00, 2004-07-12 00:00:00, 2004-07-13 00:00:00, 2004-07-14 00:00:00, 2004-07-15 00:00:00, 2004-07-16 00:00:00, 2004-07-19 00:00:00, 2004-07-20 00:00:00, 2004-07-21 00:00:00, 2004-07-22 00:00:00, 2004-07-23 00:00:00, 2004-07-26 00:00:00, 2004-07-27 00:00:00, 2004-07-28 00:00:00, 2004-07-29 00:00:00, 2004-07-30 00:00:00, 2004-08-02 00:00:00, 2004-08-03 00:00:00, 2004-08-04 00:00:00, 2004-08-05 00:00:00, 2004-08-06 00:00:00, 2004-08-09 00:00:00, 2004-08-10 00:00:00, 2004-08-11 00:00:00, 2004-08-12 00:00:00, 2004-08-13 00:00:00, 2004-08-16 00:00:00, 2004-08-17 00:00:00, 2004-08-18 00:00:00, 2004-08-19 00:00:00, 2004-08-20 00:00:00, 2004-08-23 00:00:00, 2004-08-24 00:00:00, 2004-08-25 00:00:00, 2004-08-26 00:00:00, 2004-08-27 00:00:00, 2004-08-30 00:00:00, 2004-08-31 00:00:00, 2004-09-01 00:00:00, 2004-09-02 00:00:00, 2004-09-03 00:00:00, 2004-09-06 00:00:00, 2004-09-07 00:00:00, 2004-09-08 00:00:00, 2004-09-09 00:00:00, 2004-09-10 00:00:00, 2004-09-13 00:00:00, 2004-09-14 00:00:00, 2004-09-15 00:00:00, 2004-09-16 00:00:00, 2004-09-17 00:00:00, 2004-09-20 00:00:00, 2004-09-21 00:00:00, 2004-09-22 00:00:00, 2004-09-23 00:00:00, 2004-09-24 00:00:00, 2004-09-27 00:00:00, 2004-09-28 00:00:00, 2004-09-29 00:00:00, 2004-09-30 00:00:00, 2004-10-01 00:00:00, 2004-10-04 00:00:00, 2004-10-05 00:00:00, 2004-10-06 00:00:00, 2004-10-07 00:00:00, 2004-10-08 00:00:00, 2004-10-11 00:00:00, 2004-10-12 00:00:00, 2004-10-13 00:00:00, 2004-10-14 00:00:00, 2004-10-15 00:00:00, 2004-10-18 00:00:00, 2004-10-19 00:00:00, 2004-10-20 00:00:00, 2004-10-21 00:00:00, 2004-10-22 00:00:00, 2004-10-25 00:00:00, 2004-10-26 00:00:00, 2004-10-27 00:00:00, 2004-10-28 00:00:00, 2004-10-29 00:00:00, 2004-11-01 00:00:00, 2004-11-02 00:00:00, 2004-11-03 00:00:00, 2004-11-04 00:00:00, 2004-11-05 00:00:00, 2004-11-08 00:00:00, 2004-11-09 00:00:00, 2004-11-10 00:00:00, 2004-11-11 00:00:00, 2004-11-12 00:00:00, ...]
Index: []

Can someone help me to get what I'm missing?

  • merge both df's then calculate sum – Anurag Dabas Aug 04 '21 at 09:46
  • 1
    if they have the same columns, just add them ``A + B``, pandas will align on the indices before summing – sammywemmy Aug 04 '21 at 09:50
  • In you code I cannot see the different dataframes you want to sum up. I only see 1 with external data, which is signals. The issue probably arises from that dataframe, but I do not know what it is like. Your general question is quite simple, if all indices allign you can just do: ```total_daily_returns["Total"] = sum([i["Returns"] for i in list_of_dataframes])``` – Alfred Rodenboog Aug 04 '21 at 09:57

0 Answers0