b4shyou
b4shyou

Reputation: 307

-= works different in numpy?

When I use:

f = f - df

everything works. Hence:

f-=df

results in this error:

UFuncTypeError: Cannot cast ufunc 'subtract' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

Does the -= operator work any different in numpy?

Upvotes: 0

Views: 49

Answers (2)

Michaelborn
Michaelborn

Reputation: 26

When you use var3 = var1 - var2, python constructs a new object var3 to which casting rules are applied.

However, in the var2 -= var 1 example, there are only two objects, hence the cast has to be explicit.

Upvotes: 1

zariiii9003
zariiii9003

Reputation: 356

I assume this is what is happening here:

-= is an inplace operation for the mutable object f. The inplace operation does not work in this case, because it would change the datatype of f.

Upvotes: 1

Related Questions