title: Numpy append: Automatically cast an array of the wrong dimension
body: <p>is there a way to do the following without an if clause?</p>
  
  <p>I'm reading a set of netcdf files with pupynere and want to build an array with numpy append. Sometimes the input data is multi-dimensional (see variable "a" below), sometimes one dimensional ("b"), but the number of elements in the first dimension is always the same ("9" in the example below). </p>
  
  <pre><code>&gt; import numpy as np
  &gt; a = np.arange(27).reshape(3,9)
  &gt; b = np.arange(9)
  &gt; a.shape
  (3, 9)
  &gt; b.shape
  (9,)
  </code></pre>
  
  <p>this works as expected:</p>
  
  <pre><code>&gt; np.append(a,a, axis=0)
  array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
     [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
     [18, 19, 20, 21, 22, 23, 24, 25, 26],
     [ 0,  1,  2,  3,  4,  5,  6,  7,  8],
     [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
     [18, 19, 20, 21, 22, 23, 24, 25, 26]])
  </code></pre>
  
  <p>but, appending b does not work so elegantly:</p>
  
  <pre><code>&gt; np.append(a,b, axis=0)
  ValueError: arrays must have same number of dimensions
  </code></pre>
  
  <p>The problem with append is (from the numpy manual)</p>
  
  <blockquote>
    <p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html" rel="nofollow">"When axis is specified, values must have the correct shape."</a></p>
  </blockquote>
  
  <p>I'd have to cast first in order to get the right result.</p>
  
  <pre><code>&gt; np.append(a,b.reshape(1,9), axis=0)
  array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
     [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
     [18, 19, 20, 21, 22, 23, 24, 25, 26],
     [ 0,  1,  2,  3,  4,  5,  6,  7,  8]])
  </code></pre>
  
  <p>So, in my file reading loop, I'm currently using an if clause like this:</p>
  
  <pre><code>for i in [a, b]:
      if np.size(i.shape) == 2:
          result = np.append(result, i, axis=0)
      else:
          result = np.append(result, i.reshape(1,9), axis=0)
  </code></pre>
  
  <p>Is there a way to append "a" and "b" without the if statement?</p>
  
  <p><strong>EDIT:</strong> While @Sven answered the original question perfectly (using <code>np.atleast_2d()</code>), he (and others) pointed out that the code is inefficient. In an answer below, I combined their suggestions and replaces my original code. It should be much more efficient now. Thanks.</p>