So I'm trying to do the following:
self.cashflows["Balance"] * self.assumptions["Default Rates"][current_period - 1]
where cashflows is a list with python floats and assumptions is a list with numpy floats.
I used numpy to fill the assumption vector and I am getting the following error when I try to multiply the two:
can't multiply sequence by non-int of type 'numpy.float64
I get the error but what would be my best course of action here?
thx
-
1Is the left side of that multiplication a numpy array or an ordinary list or tuple? Python doesn't support multiplication of a list by a numpy.float64. You'll need to convert the list into a numpy array.Frank Yellin– Frank Yellin2020-10-26 19:40:08 +00:00Commented Oct 26, 2020 at 19:40
-
@FrankYellin. Seems like a list. Result would be a bit unexpected if this workedMad Physicist– Mad Physicist2020-10-26 20:00:07 +00:00Commented Oct 26, 2020 at 20:00
1 Answer
Depends on what you want to do. Do you want to multiply every item in the list self.cashflow["Balance"] with self.assumptions["Default Rates"][current_period - 1]? Then you can use some list comprehension:
result = [q * self.assumptions["Default Rates"][current_period - 1] for q in self.cashflow["Balance"]]
or convert your second argument to np.float:
result = self.assumptions["Default Rates"][current_period - 1]* np.asarray(self.cashflow["Balance"])
Otherwise, multiplying a whole list by N repeats that list N times. If thats what you want, cast your np.float64 to int.
EDIT: Added missing multiplication sign
2 Comments
item method