0

I've got two dataframes of equal dimensions and I simply want to multiply them to get a new dataframe containing the product of their elements. However, instead I'm getting 6 columns of nothing but "NaN"s...

These are the two dataframes:

>>> period_subset_data
               SP500    NASDAQ      US20
1999-08-02  0.004698 -0.005727  0.000751
1999-08-03 -0.005810 -0.007322 -0.002250
...              ...       ...       ...
2015-04-22  0.002943  0.002129 -0.011747

[4103 rows x 3 columns]

>>> facmat
               SP500    NASDAQ      US20
1999-08-02  0.979367  0.670188  1.696232
1999-08-03  0.979367  0.670188  1.696232
...              ...       ...       ...
2015-04-21  0.979367  0.670188  1.696232
2015-04-22  0.979367  0.670188  1.696232

[4103 rows x 3 columns]

When I try to multiply them, this is what I get: norm_returns=period_subset_data*facmat

>>> norm_returns
            SP500  NASDAQ  US20  (SP500,)  (NASDAQ,)  (US20,)
1999-08-02    NaN     NaN   NaN       NaN        NaN      NaN
1999-08-03    NaN     NaN   NaN       NaN        NaN      NaN
...           ...     ...   ...       ...        ...      ...
2015-04-21    NaN     NaN   NaN       NaN        NaN      NaN
2015-04-22    NaN     NaN   NaN       NaN        NaN      NaN

[4103 rows x 6 columns]

I'm running code written by someone else that apparently worked for them, so I'm struggling to understand why it isn't working for me...

cmac
  • 13
  • 2

2 Answers2

1

You can try the following as you need to multiply based on the index. I found this answer from here.

pd.DataFrame(df.values*df2.values, columns=df.columns, index=df.index)

Naveen
  • 1,190
  • 7
  • 20
0

it's a very common problem, the easiest way around it is to do:

norm_returns = period_subset_data*facmat.values

However, I tried doing just

norm_returns = period_subset_data*facmat

and it worked just fine with the sample data you gave us.

Yuca
  • 6,010
  • 3
  • 22
  • 42
  • just a piece of advice, `.values` will work as long as shapes are compatible, but there is no guarantee that you're multiplying on the correct dates, for example at position 10 you may have 1990-01-20 for df1 and 1990-01-22 for df2. Good luck! – Yuca Oct 04 '18 at 21:36