0

I am doing something fairly simple, I'm trying to convert a list of lists to an np.array:

rows = [[1, 'None', 'None', 0, 0, 'None', 0, 0], 
        [2, 'None', 'None', 0, 0,    'None', 0, 0], 
        [3, 'None', 'None', 0, 0, 'None', 0, 0], 
        [4, 'None', 'None', 0, 0, 'None', 0, 0], 
        [5, 'None', 'None', 0, 0, 'None', 0, 0]]

dtypes = np.dtype(
    [
        ('_ID', np.int),
        ('SOURCE_LILST', '|S1024'),
        ('PRI_SOURCE', '|S256'),
        ('PRI_SOURCE_CNT', np.int32),
        ('PRI_SOURCE_PER', np.float64),
        ('SEC_SOURCE', '|S256'),
        ('SEC_SOURCE_CNT', np.int32),
        ('SEC_SOURCE_PER', np.float64)
    ]
)
array = np.array(rows, dtypes) # error raised

Can anyone see what the issue it?

Thank you

1 Answer 1

2

Your rows object should be a list of tuples, not a list of lists:

rows = [(1, 'None', 'None', 0, 0, 'None', 0, 0), 
        (2, 'None', 'None', 0, 0,    'None', 0, 0), 
        (3, 'None', 'None', 0, 0, 'None', 0, 0), 
        (4, 'None', 'None', 0, 0, 'None', 0, 0), 
        (5, 'None', 'None', 0, 0, 'None', 0, 0)]

dtypes = np.dtype(
    [
        ('_ID', np.int),
        ('SOURCE_LILST', '|S1024'),
        ('PRI_SOURCE', '|S256'),
        ('PRI_SOURCE_CNT', np.int32),
        ('PRI_SOURCE_PER', np.float64),
        ('SEC_SOURCE', '|S256'),
        ('SEC_SOURCE_CNT', np.int32),
        ('SEC_SOURCE_PER', np.float64)
    ]
)
array = np.array(rows, dtypes) # no error raised

A few extra hints:

  1. Try to simplify your problem as much as possible to figure out where the issue is. In this case, you could have deleted all but one of the columns, and hence dtypes, and still seen the problem. This helps eliminate possible errors like whether or not you specified all those strings correctly, etc.
  2. Google your problem before bothering to create a question.
  3. When asking a question about why you're getting an error, include the error message — or at least some shortened version of it.

More generally, read this. It's not just helpful for asking better questions, it's also a helpful way to figure out problems before you even get around to asking.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.