Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Division-assignment operator raises error, separate division and assignment work #10565

Closed
werenike opened this issue Feb 9, 2018 · 3 comments

Comments

@werenike
Copy link
Contributor

werenike commented Feb 9, 2018

Sorry if this is a known issue, I searched for it but there are so many results...

This works:

import numpy as np
a = np.array([1, 2, 3])
a = a / 4

But this raises an error:

import numpy as np
a = np.array([1, 2, 3])
a /= 4

Namely:

TypeError: No loop matching the specified signature and casting
was found for ufunc true_divide

This is surprising since I would expect them to do the same.

Using numpy version 1.14.0.

@rkern
Copy link
Member

rkern commented Feb 9, 2018

I assume that you are on Python 3. This is expected behavior on Python 3. The result of a / 4 will not be an integer array but a floating point array. When you simply reassign that result with the line a = a / 4, that's fine. It essentially converts the code to a = np.true_divide(a, 4) which creates a new floating point array and reassigns the name a to it.

When you try to do in-place assignment, the true_divide ufunc is being asked to output back into the existing a integer array: np.true_divide(a, 4, out=a). There is no implementation of true_divide that takes two integer arguments and outputs another integer, so you get the exception.

@werenike
Copy link
Contributor Author

werenike commented Feb 9, 2018

Thanks for the clear explanation, that makes sense. The below helped me to understand the difference:

>>> a = [1 ,2, 3]
>>> b = a
>>> id(a) == id(b)
True
>>> a += [4, 5, 6]  # list object is changed, no new object created
>>> id(a) == id(b)
True
>>> a = a + [4, 5, 6]  # new object created, and assigned to `a`
>>> id(a) == id(b)
False

If I understand correctly, the fact that no new object is created with +=, is the analogy to out=a.

One way around the issue is to use dtype=float upon array creation.

@rkern
Copy link
Member

rkern commented Feb 9, 2018

That's right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants