-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
astype converts numpy array values to 0.0 for structured dtype #7058
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
Comments
Also in 1.11. |
I will check when I get home, but I suspect this is another version of the long-standing bug which could be fixed by #6053. Specifically, I think this is example 'v3' in the example there: When numpy does not find a matching field-name on the lhs, it simply assigns 0 to the rhs field. |
I'd also add, @mpconte's example is in some sense the current desired behavior: Assignment between two structures arrays currently works by matching up field names between the lhs and rhs. If a field name on the rhs is not present in the lhs, that field gets assigned 0. This is not documented anywhere, but there are comments in the code suggesting it is intentional. If you want a real mind-bender, replaced
|
That makes sense. |
Thanks for the insight. |
This commit attempts to make structure assignment more consistent, and then changes multi-field indices to return a view instead of a copy. Assignment between structures now works "by field position" rather than "by field name". Fixes numpy#2353, fixes numpy#6085, fixes numpy#3351, fixes numpy#6085, fixes numpy#6314, fixes numpy#2346, fixes numpy#7058, fixes numpy#3641, fixes numpy#5994, fixes numpy#7262, fixes numpy#7493
This commit attempts to make structure assignment more consistent, and then changes multi-field indices to return a view instead of a copy. Assignment between structures now works "by field position" rather than "by field name". Fixes numpy#2353, fixes numpy#6085, fixes numpy#3351, fixes numpy#6085, fixes numpy#6314, fixes numpy#2346, fixes numpy#7058, fixes numpy#3641, fixes numpy#5994, fixes numpy#7262, fixes numpy#7493
import numpy as np
dtype = np.dtype([('Point', 'f', (3,))])
ones = np.ones(1, dtype=dtype) --> array([([1.0, 1.0, 1.0],)], dtype=[('Point', '<f4', (3,))])
flattened_dtype = np.dtype([('x', 'f'), ('y', 'f'), ('z', 'f')])
flat_ones = ones.astype(flattened_dtype) --> array([(0.0, 0.0, 0.0)], dtype=[('x', '<f4'), ('y', '<f4'), ('z', '<f4')])
numpy version 1.9.2
The text was updated successfully, but these errors were encountered: