0

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

2
  • 1
    Is 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. Commented Oct 26, 2020 at 19:40
  • @FrankYellin. Seems like a list. Result would be a bit unexpected if this worked Commented Oct 26, 2020 at 20:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

For the last part, use the item method
Thanks - I was just targeting one element!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.