The default recarray
iterator seems to be going over columns. Is there a counterpart to pandas’es iterrows
/itertuples
that iterate row views?
Asked
Active
Viewed 344 times
0

Roman Shapovalov
- 2,785
- 1
- 25
- 30
-
What's the shape of this array? While you are at it, what's the `dtype`? – hpaulj Oct 07 '19 at 18:14
-
recarrays don’t really have shape. dtype is `[('fieldname', 'O')] * n_fields`. – Roman Shapovalov Oct 07 '19 at 18:47
-
A `record` has the dtype, but `recarray`, a variation on a structured array has shape as well. Columns and rows make sense when the array is 2d. – hpaulj Oct 07 '19 at 19:02
-
A small example might help. – hpaulj Oct 07 '19 at 20:07
1 Answers
-2
In [102]: np.dtype('O,O,O')
Out[102]: dtype([('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
In [103]: dt = np.dtype('O,O,O')
In [104]: np.recarray((4,), dt)
Out[104]:
rec.array([(None, None, None), (None, None, None), (None, None, None),
(None, None, None)],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
In [105]: arr = np.recarray((4,), dt)
In [106]: arr
Out[106]:
rec.array([(None, None, None), (None, None, None), (None, None, None),
(None, None, None)],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
In [107]: arr.f0
Out[107]: array([None, None, None, None], dtype=object)
In [108]: arr['f0']
Out[108]: array([None, None, None, None], dtype=object)
In [109]: for i in arr: print(repr(i))
(None, None, None)
(None, None, None)
(None, None, None)
(None, None, None)
In [110]: arr = np.recarray((4,1), dt)
In [111]: arr
Out[111]:
rec.array([[(None, None, None)],
[(None, None, None)],
[(None, None, None)],
[(None, None, None)]],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
In [112]: for i in arr: print(repr(i))
rec.array([(None, None, None)],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
rec.array([(None, None, None)],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
rec.array([(None, None, None)],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])
rec.array([(None, None, None)],
dtype=[('f0', 'O'), ('f1', 'O'), ('f2', 'O')])

hpaulj
- 221,503
- 14
- 230
- 353
-
This clearly shows that the OP is wrong. A recarray with shape (4,) iterates 4 times, producing a record each time. This array does not have 'columns'. Iterating on fields requires a different operation. – hpaulj Jun 18 '23 at 18:50