0

This question has been edited as to make more sense.

The original question is how to insert values into a numpy record array, and I have had som success but still have an issue. Based off of the website below I have been inserting values into a record array.

Python code

instance_format={
    'names' : ('name','offset'),
    'formats' : ('U100','U30')}

instance=np.zeros(20,dtype=instance_format)

#I am placing values in the array similar to this
instance[0]['name']="Wire 1"
instance[1]['name']="Wire 2"
instance[2]['name']="Wire 3"


instance[0]['offset']="0x103"
instance[1]['offset']="0x104"
instance[2]['offset']="0x105"

#Here is the insertion statement that works
instance1 = np.insert(instance1,1,"Module one")

print(instance1)

Output

[('One Wire 1', '0x103')
 ('Module One',  'Module One')
 ('One Wire 2', '0x104')
 ('One Wire 3', '0x105')

So the insert statement works, however it inserts it both in the name and the offset field. I want to insert it just in the name field. How do I this?

Thanks

2 Answers 2

1

Your instance

In [470]: instance
Out[470]: 
array([('', '', ''), ('', '', ''), ('', '', ''), ('', '', ''),
       ('', '', ''), ('', '', ''), ('', '', ''), ('', '', ''),
       ('', '', ''), ('', '', ''), ('', '', ''), ('', '', ''),
       ('', '', ''), ('', '', ''), ('', '', ''), ('', '', ''),
       ('', '', ''), ('', '', ''), ('', '', ''), ('', '', '')], 
      dtype=[('name', '<U100'), ('module', '<U100'), ('offset', '<U30')])

does not look like

 ['One Wire Instance 1', 'One Wire Instance 2', 'One Wire Instance 3']

Are you talking about one record of instance, which would display as

 ('One Wire Instance 1', 'One Wire Instance 2', 'One Wire Instance 3')

with each string being the name, module, and offset.

Or are these 3 strings e.g. instance['name'][:3], the 'name' field from 3 records?

Inserting a new record into the instance array is one thing, adding a new field to the array is quite another.


To use np.insert with a structured array, you need provide a 1 element array with the correct dtype.

With your new instance:

In [580]: newone = np.array(("module one",'',''),dtype=instance.dtype)
In [581]: newone
Out[581]: 
array(('module one', '', ''), 
      dtype=[('name', '<U100'), ('module', '<U100'), ('offset', '<U30')])

In [582]: np.insert(instance,1,newone)
Out[582]: 
array([('Wire 1', '', '0x103'), ('module one', '', ''),
       ('Wire 2', '', '0x104'), ('Wire 3', '', '0x105')], 
      dtype=[('name', '<U100'), ('module', '<U100'), ('offset', '<U30')])

np.insert is just a function that performs these steps:

In [588]: instance2 = np.zeros((4,),dtype=instance.dtype)
In [589]: instance2[:1]=instance[:1]
In [590]: instance2[2:]=instance[1:3]
In [591]: instance2
Out[591]: 
array([('Wire 1', '', '0x103'), ('', '', ''), ('Wire 2', '', '0x104'),
       ('Wire 3', '', '0x105')], 
      dtype=[('name', '<U100'), ('module', '<U100'), ('offset', '<U30')])
In [592]: instance2[1]=newone
In [593]: instance2
Out[593]: 
array([('Wire 1', '', '0x103'), ('module one', '', ''),
       ('Wire 2', '', '0x104'), ('Wire 3', '', '0x105')], 
      dtype=[('name', '<U100'), ('module', '<U100'), ('offset', '<U30')])

It creates a new array of the correct target size, copies elements from the original array, and puts the new array into the empty slot.

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

1 Comment

I added an example of how to use insert.
0

I can't understand what you mean by:

I want to insert the name "Reserved" in the second element which would make the array have the following contents ['One Wire Instance 1','Reserved' , 'One Wire Instance 2', 'One Wire Instance 3']

Do you want:

instance[1] = 'Reserved','', ''

?

1 Comment

Hi please check the edits, and since arrays are indexed at 0 it would be instance[1]= ' ','Reserved'

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.