Numpy, Add Column to Existing Structured Array
I have an initial array as such:
[(1, [-112.01268501699997, 40.64249414272372])
(2, [-111.86145708699996, 40.4945008710162])]
where the first column is int and the second is a tuple with floats. I need to add a USNG string column.
Then I create a numpy structured array as such:
dtype = numpy.dtype([('USNG', '|S100')])
x = numpy.empty(array.shape, dtype=dtype)
I want to add an x numpy array to an existing one to add a new column so that I can output some information to that column for each row. When I do the following:
numpy.append(array, x, axis=1)# I've also tried vstack and hstack
I am getting the following error:
'TypeError: invalid type promotion'
Any suggestions on why this is happening?
thank
source to share
You need to create a new dtype containing the new field.
For example, here a
:
In [86]: a
Out[86]:
array([(1, [-112.01268501699997, 40.64249414272372]),
(2, [-111.86145708699996, 40.4945008710162])],
dtype=[('i', '<i8'), ('loc', '<f8', (2,))])
a.dtype.descr
- [('i', '<i8'), ('loc', '<f8', (2,))]
; those. list of field types. We will create a new dtype by adding ('USNG', 'S100')
to the end of this list:
In [87]: new_dt = np.dtype(a.dtype.descr + [('USNG', 'S100')])
Now create a new structured array b
. Here I have used zeros
so string fields start with value ''
. You can also use empty
. Then the lines will contain garbage, but it doesn't matter if you assign values to them right away.
In [88]: b = np.zeros(a.shape, dtype=new_dt)
Copy existing data from a
to b
:
In [89]: b['i'] = a['i']
In [90]: b['loc'] = a['loc']
Here b
now:
In [91]: b
Out[91]:
array([(1, [-112.01268501699997, 40.64249414272372], ''),
(2, [-111.86145708699996, 40.4945008710162], '')],
dtype=[('i', '<i8'), ('loc', '<f8', (2,)), ('USNG', 'S100')])
Fill in a new field with some data:
In [93]: b['USNG'] = ['FOO', 'BAR']
In [94]: b
Out[94]:
array([(1, [-112.01268501699997, 40.64249414272372], 'FOO'),
(2, [-111.86145708699996, 40.4945008710162], 'BAR')],
dtype=[('i', '<i8'), ('loc', '<f8', (2,)), ('USNG', 'S100')])
source to share
The question is, "Any suggestions on why this is happening?"
This is essentially a bug - it has been an open ticket with numpy since 2012.
source to share