1

I want to construct an numpy-array with various different data types using numpy.dtype() object.

I have a dictionary 'mydict' where all the information about the data is stored, I want to create a data type object dt with it. 'mydict' is dynamically created depending on which properties I am choosing and the data type is corresponding to the property as well!

import numpy as np

mydict={'name0': 'mass', 'name1': 'position', 'name2': 'ID', 
      'format0': np.float32, 'format1': np.int8, 'format2': np.uint64}

the data type object dt should look like this

dt = np.dtype([('mass', np.float32), ('position', np.int8), ('ID', np.uint64)])

My question is how to create dt without constructing/writing it manually into the code?

The main problem is that I do not know how to append np.dtype() with another entry of 'name' and 'format' combination or if that is even possible ...

I am using dt then to read my data into a numpy array like this!

data_array=np.array((nr_rows, nr_cols)), dtype=dt)

I tried certain attempts with Dict Comprehensions, lists and dictionaries but I could not find the right way to do that.

4
  • Where did you get the dict? You may want to construct it with your desired dtype in mind, so that you don't have to restructure it. Commented Mar 7, 2017 at 18:30
  • @Psidom: Hi, yes I want to do so, this dict is only an example! Commented Mar 7, 2017 at 18:31
  • Are the keys of the dict following a pattern as you are showing? name[0-9] and format[0-9]. Commented Mar 7, 2017 at 18:38
  • @Psidom: Yes, exactly! Commented Mar 7, 2017 at 18:42

2 Answers 2

1
In [209]: dd={'name0': 'mass', 'name1': 'position', 'name2': 'ID', 
     ...:       'format0': np.float32, 'format1': np.int8, 'format2': np.uint64}

In [213]: [(dd['name%s'%i],dd['format%s'%i]) for i in range(3)]
Out[213]: [('mass', numpy.float32), ('position', numpy.int8), ('ID', numpy.uint64)]

In [214]: dt=np.dtype([(dd['name%s'%i],dd['format%s'%i]) for i in range(3)])
In [216]: arr = np.zeros((2,), dt)
In [217]: arr
Out[217]: 
array([( 0., 0, 0), ( 0., 0, 0)], 
      dtype=[('mass', '<f4'), ('position', 'i1'), ('ID', '<u8')])
Sign up to request clarification or add additional context in comments.

Comments

0

If your keys follow strict patterns as in the question, you can take a look at this; 1) extract values where the keys start with name and set them as the first element of the tuple; 2) replace name with format, and extract corresponding value as the second element of the tuple; 3) construct the dtype from the list of tuples.

import numpy as np
np.dtype([(d[k], d[k.replace('name', 'format')]) for k in d.keys() if k.startswith('name')])

# dtype([('position', 'i1'), ('ID', '<u8'), ('mass', '<f4')])

Note: you may need an OrderDict initially in order to have the right order of columns here.

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.