[Numpy-discussion] numpy.concatenate doesn't check axis for 1D case. Bug or feature?

Stuart Brorson sdb@cloud9....
Thu Jan 24 10:26:02 CST 2008


As the subject says, numpy.concatenate doesn't seem to obey -- or even
check -- the axis flag when concatenating 1D arrays:

-----------------------  <session log>  -----------------

In [30]: A = numpy.array([1, 2, 3, 4])

In [31]: D = numpy.array([6, 7, 8, 9])

In [32]: numpy.concatenate((A, D))
Out[32]: array([1, 2, 3, 4, 6, 7, 8, 9])

In [33]: numpy.concatenate((A, D), axis=0)
Out[33]: array([1, 2, 3, 4, 6, 7, 8, 9])

In [34]: numpy.concatenate((A, D), axis=1)
Out[34]: array([1, 2, 3, 4, 6, 7, 8, 9])

In [35]: numpy.concatenate((A, D), axis=2)
Out[35]: array([1, 2, 3, 4, 6, 7, 8, 9])

-----------------------  </session log>  -----------------

However, if you create the same arrays as 2D (1xn) arrays, then numpy
checks, and does the right thing:

----------------------  <session log>  -------------------

In [36]: A = numpy.array([[1, 2, 3, 4]])

In [37]: D = numpy.array([[6, 7, 8, 9]])

In [38]: A.shape
Out[38]: (1, 4)

In [39]: numpy.concatenate((A, D))
Out[39]: 
array([[1, 2, 3, 4],
        [6, 7, 8, 9]])

In [40]: numpy.concatenate((A, D), axis=0)
Out[40]: 
array([[1, 2, 3, 4],
        [6, 7, 8, 9]])

In [41]: numpy.concatenate((A, D), axis=1)
Out[41]: array([[1, 2, 3, 4, 6, 7, 8, 9]])

In [42]: numpy.concatenate((A, D), axis=2)
---------------------------------------------------------------------------
<type 'exceptions.ValueError'>            Traceback (most recent call
last)

/fs/home/sdb/<ipython console> in <module>()

<type 'exceptions.ValueError'>: bad axis1 argument to swapaxes

-----------------------  </session log>  -----------------

Question:  Is is a bug or a feature?

I'd at least think that numpy would check the axis arg in the 1D case,
and issue an error if the user tried to do something impossible
(e.g. axis=2).

Whether numpy would promote a 1D to a 2D array is a different
question, and I am agnostic about that one.

Cheers,

Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA
http://www.interactivesupercomputing.com/



More information about the Numpy-discussion mailing list