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

BUG: np.rec.fromarrays(...) may fail if a field contains arrays of the same shape[0] #22755

Open
herrlich10 opened this issue Dec 7, 2022 · 1 comment

Comments

@herrlich10
Copy link

Describe the issue:

np.rec.fromarrays(...) may fail if a field contains arrays of the same shape[0]. The error will not occur if the arrays are of different shape[0], e.g., [np.zeros(2), np.ones(3)]. The error will not occur if written as np.array([(np.zeros(2), 'a'), (np.ones(2), 'b')], dtype=[('field1', object), ('field2', '<U1')]).

Reproduce the code example:

import numpy as np
np.rec.fromarrays([[np.zeros(2), np.ones(2)], ['a', 'b']], names=['field1', 'field2'])

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-eecf8753473e> in <module>
      1 import numpy as np
----> 2 np.rec.fromarrays([[np.zeros(2), np.ones(2)], ['a', 'b']], names=['field1', 'field2'])

~/PythonPlus/anaconda3/lib/python3.9/site-packages/numpy/core/records.py in fromarrays(arrayList, dtype, shape, formats, names, titles, aligned, byteorder)
    679         testshape = obj.shape[:obj.ndim - nn]
    680         if testshape != shape:
--> 681             raise ValueError("array-shape mismatch in array %d" % k)
    682 
    683     _array = recarray(shape, descr)

ValueError: array-shape mismatch in array 1

NumPy/Python version information:

1.20.3 3.9.7 (default, Sep 16 2021, 08:50:36)
[Clang 10.0.0 ]

Context for the issue:

No response

@seberg
Copy link
Member

seberg commented Dec 7, 2022

Note that unlike 1.20.x the call also fails if the shapes of the arrays do not match on newer versions. So I suspect if you want the object field here, more you have to expect to do extra steps to get it.
I am unsure whether using shape and dtype or similar shouldn't work though, so I suspect there are enhancements possible, I just suspect the error in itself is OK, since the discrepancy is gone.

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

2 participants