Skip to content

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

Closed
mpconte opened this issue Jan 18, 2016 · 5 comments
Closed

astype converts numpy array values to 0.0 for structured dtype #7058

mpconte opened this issue Jan 18, 2016 · 5 comments

Comments

@mpconte
Copy link

mpconte commented Jan 18, 2016

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

@charris
Copy link
Member

charris commented Jan 18, 2016

Also in 1.11.

@ahaldane
Copy link
Member

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.

@ahaldane
Copy link
Member

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 flattened_dtype in your example by

flattened_dtype = np.dtype([('x', 'f'), ('Point', 'f'), ('z', 'f')])

@charris
Copy link
Member

charris commented Jan 18, 2016

That makes sense.

@mpconte
Copy link
Author

mpconte commented Jan 19, 2016

Thanks for the insight.
I also just discovered that using view instead of astype acutally gives me the result I'd expect, regardless of field names, thanks.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 17, 2016
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
@charris charris closed this as completed in 7412b85 Sep 9, 2017
theodoregoetz pushed a commit to theodoregoetz/numpy that referenced this issue Oct 23, 2017
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants