question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot iterate over fields of record array

See original GitHub issue

When iterating over the fields of a record array, numba complains about a not implemented getitem. Accessing the fields by name outside a loop works. I am using numba 0.52, the problem occurs on both Windows and linux

This is the ipython cell that produces the error:

%pylab
import numpy.random
from numba import njit

@njit
def recarriterator(rec):
    for o in 'a','b','c':
        print(rec[o][0])
    return
        
@njit
def recarriterator2(rec):
    print(rec['a'][0])
    print(rec['b'][0])
    print(rec['c'][0])
    return
        
ldd=[]
ldk=[]
keys='a','b','c'
for o in keys:
    #if not (o == 'observation_id' or o == 'report_id' or o == 'sensor_id' or o == 'source_id'):
        ldd.append(random.rand(5))
        ldk.append((o,ldd[-1].dtype))
rec = numpy.rec.fromarrays(ldd, dtype=ldk)
    
recarriterator2(rec)
recarriterator(rec)

The error code:

Using matplotlib backend: Qt5Agg
Populating the interactive namespace from numpy and matplotlib
0.26156099390229226
0.4919898206995005
0.363624558244539
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-1-56c6c55bddcf> in <module>
     26 
     27 recarriterator2(rec)
---> 28 recarriterator(rec)

~\anaconda3\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
    413                 e.patch_message(msg)
    414 
--> 415             error_rewrite(e, 'typing')
    416         except errors.UnsupportedError as e:
    417             # Something unsupported is present in the user code, add help info

~\anaconda3\lib\site-packages\numba\core\dispatcher.py in error_rewrite(e, issue_type)
    356                 raise e
    357             else:
--> 358                 reraise(type(e), e, None)
    359 
    360         argtypes = []

~\anaconda3\lib\site-packages\numba\core\utils.py in reraise(tp, value, tb)
     78         value = tp()
     79     if value.__traceback__ is not tb:
---> 80         raise value.with_traceback(tb)
     81     raise value
     82 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:
 
 >>> getitem(unaligned array(Record(a[type=float64;offset=0],b[type=float64;offset=8],c[type=float64;offset=16];24;False), 1d, C), unicode_type)
 
There are 22 candidate implementations:
  - Of which 20 did not match due to:
  Overload of function 'getitem': File: <numerous>: Line N/A.
    With argument(s): '(unaligned array(Record(a[type=float64;offset=0],b[type=float64;offset=8],c[type=float64;offset=16];24;False), 1d, C), unicode_type)':
   No match.
  - Of which 2 did not match due to:
  Overload in function 'GetItemBuffer.generic': File: numba\core\typing\arraydecl.py: Line 162.
    With argument(s): '(unaligned array(Record(a[type=float64;offset=0],b[type=float64;offset=8],c[type=float64;offset=16];24;False), 1d, C), unicode_type)':
   Rejected as the implementation raised a specific error:
     TypeError: unsupported array index type unicode_type in [unicode_type]
  raised from C:\Users\leopo\anaconda3\lib\site-packages\numba\core\typing\arraydecl.py:68

During: typing of intrinsic-call at <ipython-input-1-56c6c55bddcf> (8)

File "<ipython-input-1-56c6c55bddcf>", line 8:
def recarriterator(rec):
    <source elided>
    for o in 'a','b','c':
        print(rec[o][0])
        ^

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, May 16, 2022

This should work:

import numpy as np
import numba as nb
from numba import literal_unroll

dt = np.dtype([("a", np.uint8), ("b", np.uint8)])
dt_names = dt.names

@nb.njit
def test():
    rec = np.zeros(1, dtype=dt)
    for f in literal_unroll(dt_names):
        rec[0][f] = 1
    return rec

print(test())

note that literal_unroll is imported explicitly. This is a known bug, think it’s a duplicate of https://github.com/numba/numba/issues/5344.

0reactions
esccommented, May 16, 2022

@pwuertz thank you for submitting this. I can confirm that I am able to reproduce on current main (3781d6836d78f3467b02500b19a889039bdaf3bb) with the following error:

Traceback (most recent call last):
  File "/Users/vhaenel/git/numba/issue_6634.py", line 14, in <module>
    test()
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/Users/vhaenel/git/numba/numba/core/dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function setitem>) found for signature:

 >>> setitem(Record(a[type=uint8;offset=0],b[type=uint8;offset=1];2;False), unicode_type, Literal[int](1))

There are 16 candidate implementations:
  - Of which 16 did not match due to:
  Overload of function 'setitem': File: <numerous>: Line N/A.
    With argument(s): '(Record(a[type=uint8;offset=0],b[type=uint8;offset=1];2;False), unicode_type, int64)':
   No match.

During: typing of setitem at /Users/vhaenel/git/numba/issue_6634.py (11)

File "issue_6634.py", line 11:
def test():
    <source elided>
    for f in nb.literal_unroll(dt_names):
        rec[0][f] = 1
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cant iterate through the array of fetched records - Stack Overflow
Cant iterate through the array of fetched records · "So, how can I iterate through" you are already iterating through data array here....
Read more >
How to iterate through an array of input fields with the same ...
Presumably, you want to update same element/object in whatever iteration of contentArray that you are updating in the other fields.
Read more >
How do I iterate through an array of results from a step? - Help
At the top of the step, you'll need to iterate over the array of results ( steps.airtable_get_tasksOutstanding.$return_value.records ) ...
Read more >
TypeError: 'x' is not iterable - JavaScript - MDN Web Docs
An iterable can be a built-in iterable type such as Array , String or Map , a ... Therefore, you cannot use for...of...
Read more >
Iterate through collections in C# | Microsoft Learn
Learn how to use an iterator to step through collections like lists and arrays. Iterators are consumed from client code using a foreach ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found