3

Suppose I have two numpy arrays x1 and x2 with the same dtype but different shape:
x1.dtype = dtype([('fmv', '<f4'), ('delta', '<f4'), ('rho', '<f4', (5,))])
x2.dtype = dtype([('fmv', '<f4'), ('delta', '<f4'), ('rho', '<f4', (5,))])


x1.shape = (10L, 1L)
x2.shape = (10L, 6L)

I would like to concatenate these two arrays on axis 1:
y = np.concatenate((x1,x2), axis=1)

This results in:
y.shape = (10L, 7L) Good
y['rho'].shape = (5L, 10L, 7L) Bad
Why did the field rho have it's shape transposed? I was expecting (10, 7, 5)

UPDATE:

               x1.flags | x2.flags | y.flags
C_CONTIGUOUS : True     | True     | False
F_CONTIGUOUS : False    | False    | True
OWNDATA      : False    | False    | False
WRITEABLE    : True     | True     | True
ALIGNED      : True     | True     | True
UPDATEIFCOPY : False    | False    | False


x1.strides = (28L, 28L)
x2.strides = (168L, 28L)
y.strides = (28L, 280L)


I made a little script you can run to duplicate the results:
import numpy as np
x = np.zeros((5,3), dtype=np.dtype([('field1','<f8'),('field2','<f8',4)]))
A1 = np.concatenate((np.array(x[0,0], ndmin=1), np.ravel(x[:,1:], order='C')), axis=0)
B1 = np.concatenate((np.tile(np.array(A1[0],ndmin=2), (5,1)), np.reshape(A1[1:], (5,2), order='C')), axis=1)
A2 = np.ravel(x, order='C')
B2 = np.reshape(A2, (5,3), order='C')

B1['field2'].shape = (4L, 5L, 3L) Bad
B2['field2'].shape = (5L, 3L, 4L) Good
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Joel Vroom
  • 1,611
  • 1
  • 16
  • 30
  • Concatenating recarrays is not that straightforward I guess. See for example [this thread](http://stackoverflow.com/questions/14847710/how-do-i-add-columns-to-an-ndarray). – Bálint Aradi Feb 14 '13 at 16:09
  • 1
    Can you share `x1.flags`, `x2.flags`, `y.flags`, `x1.strides`, `x2.strides` and `y.strides` with us? If both `x1` and `x2` are C-contiguous, it works as you were expecting for me. – Jaime Feb 14 '13 at 16:39
  • With numpy 1.6.2 it works as expected. What version are you running? – Jaime Feb 14 '13 at 17:37
  • np.version.version = '2.0.0.dev' – Joel Vroom Feb 14 '13 at 17:38
  • I see what you mean. I just ran my test script on numpy 1.6.1 and it worked as desired. – Joel Vroom Feb 14 '13 at 17:47
  • 1
    You may want to give a shout in the [numpy discussion list](http://www.scipy.org/Mailing_Lists) so they fix it. – Jaime Feb 14 '13 at 18:10
  • 2
    Seems to work fine with current versions too... 2.0.0.dev sounds like a very old development version. – seberg Feb 14 '13 at 18:36

0 Answers0