8000 BUG: array assignment to structured array entry fails in recent dev version · Issue #10036 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: array assignment to structured array entry fails in recent dev version #10036

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
paulbrodersen opened this issue Nov 16, 2017 · 2 comments
Closed

Comments

@paulbrodersen
Copy link
paulbrodersen commented Nov 16, 2017

There seems to have been a bug introduced in a recent development version in the ndarray class.
The following MWE completes and works as intended in 1.14.0.dev0+029863e / 029863e (full version / git revision) but fails with a ValueError in 1.14.0.dev0+565e8ca / 565e8ca .

MWE:

import numpy as np
arr = np.full(3, np.nan, [('a', np.float, (4)),
                          ('b', np.float, (4))])
arr[0] = np.random.rand(2,4)

Error message:

ValueError: setting an array element with a sequence.

Fluff

Tested on Ubuntu 16.04, 17.04, and 17.10 in virtual environments with only numpy, scipy, matplotlib and dependencies installed (numpy and scipy from source, rest via pip). Python versions tested: 2.7.13 and 3.5.2. Additional numpy versions tested: 1.11 (from Ubuntu/canonical repositories), for which the MWE completes successfully.

@ahaldane
Copy link
Member
ahaldane commented Nov 16, 2017

Yes, we did disable that kind of assignment. It is actually doing some very unsafe things under the hood and was probably never intended. See #3351, #6806, #6314, #7058 and the PR that fixed them, #6053. In retrospect, we should have added a deprecation warning though.

What that assignment actually does is copy from the src to dst byte-by-byte, with no account of casting, endianness, size, etc. For instance, note you can assign an array with both the wrong size and the wrong type to your array:

>>> arr[0] = arange(5, dtype='i')
>>> arr[0]
([  0.00000000e+000,   4.94065646e-324,   9.88131292e-324,   1.48219694e-323], [  1.97626258e-323,   0.00000000e+000,   0.00000000e+000,   0.00000000e+000])

In numpy 1.14, the "correct" way to do your assignment is:

>>> arr[0] = (np.random.rand(4), np.random.rand(4))

That is, you should create a tuple with each field specified separately.

PS: We also have new documentation for structured arrays, you can read the relevant section here (link) for now.

@paulbrodersen
Copy link
Author

Thanks for the quick response, and the clarification. Your otherwise customary depreciation warnings are very much appreciated. ;-) Will now update all my servers to the very newest and brightest if this behaviour is here to stay. Keep up the good work!

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
0