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

np.rec.fromarrays(...) may fail if resulting array is going to be empty #15470

Open
proste opened this issue Jan 29, 2020 · 0 comments
Open

np.rec.fromarrays(...) may fail if resulting array is going to be empty #15470

proste opened this issue Jan 29, 2020 · 0 comments

Comments

@proste
Copy link

proste commented Jan 29, 2020

Creation of np.recarray via np.rec.fromarrays(...) fails if provided arrayList are empty lists and dtype is non-trivial (containing sub-arrays).

In this particular case the function call has enough information to create empty array equivalent to output of e.g., np.empty(shape=0, dtype=dtype).view(np.recarray).

Reproducing code example:

import numpy as np

np.rec.fromarrays([[], []], dtype=[('foo', np.int32, 2), ('bar', np.int32)])

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-e2e53c33181b> in <module>
----> 1 np.rec.fromarrays([[], []], dtype=[('foo', np.int32, 2), ('bar', np.int32)])

<environment_dirname>/lib/python3.8/site-packages/numpy/core/records.py in fromarrays(arrayList, dtype, shape, formats, names, titles, aligned, byteorder)
    636         testshape = obj.shape[:obj.ndim - nn]
    637         if testshape != shape:
--> 638             raise ValueError("array-shape mismatch in array %d" % k)
    639 
    640     _array = recarray(shape, descr)

ValueError: array-shape mismatch in array 1

Numpy/Python version information:

>>> import sys, numpy; print(numpy.__version__, sys.version)
1.18.1 3.8.1 (default, Jan  8 2020, 23:09:20) 
[GCC 9.2.0]
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