0

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

Roman Shapovalov
  • 2,785
  • 1
  • 25
  • 30

1 Answers1

-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