[NumPy-Tickets] [NumPy] #2084: Issue with concatenating structured arrays

NumPy Trac numpy-tickets@scipy....
Mon Mar 19 02:40:56 CDT 2012


#2084: Issue with concatenating structured arrays
------------------------+---------------------------------------------------
 Reporter:  robitaille  |       Owner:  somebody   
     Type:  defect      |      Status:  new        
 Priority:  normal      |   Milestone:  Unscheduled
Component:  Other       |     Version:  1.6.1      
 Keywords:              |  
------------------------+---------------------------------------------------
 The following demonstrates the issue - even though Numpy knows how to
 concatenate the fields of a structured array, it crashes if one tries to
 concatenate the structured arrays themselves:

 {{{

 In [1]: import numpy as np

 In [2]: d1 = np.array(zip(['a','b','c']), dtype=[('b', '|S1')])

 In [3]: d2 = np.array(zip(['aa','bb','cc']), dtype=[('b', '|S2')])

 In [4]: np.hstack([d1['b'],d2['b']])
 Out[4]:
 array(['a', 'b', 'c', 'aa', 'bb', 'cc'],
       dtype='|S2')

 In [5]: np.hstack([d1, d2])
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call
 last)
 /Users/tom/<ipython-input-5-bd5cc420043d> in <module>()
 ----> 1 np.hstack([d1, d2])

 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
 /site-packages/numpy/core/shape_base.pyc in hstack(tup)
     268
     269     """
 --> 270     return _nx.concatenate(map(atleast_1d,tup),1)
     271

 TypeError: invalid type promotion
 }}}

 A similar issue occurs with floating-point values of different endian-
 ness:

 {{{
 In [1]: import numpy as np

 In [2]: d1 = np.array(zip([1,2,3]), dtype=[('a', '<f4')])

 In [3]: d2 = np.array(zip([1,2,3]), dtype=[('a', '>f4')])

 In [4]: np.hstack([d1['a'],d2['a']])
 Out[4]: array([ 1.,  2.,  3.,  1.,  2.,  3.], dtype=float32)

 In [5]: np.hstack([d1, d2])
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call
 last)
 /Users/tom/<ipython-input-5-bd5cc420043d> in <module>()
 ----> 1 np.hstack([d1, d2])

 /Users/tom/Library/Python/2.7/lib/python/site-
 packages/numpy/core/shape_base.pyc in hstack(tup)
     271     # As a special case, dimension 0 of 1-dimensional arrays is
 "horizontal"
     272     if arrs[0].ndim == 1:
 --> 273         return _nx.concatenate(arrs, 0)
     274     else:
     275         return _nx.concatenate(arrs, 1)

 TypeError: invalid type promotion
 }}}

 In some cases, this can even cause segmentation faults, though I have yet
 to find a way to reproduce this consistently.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/2084>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list