title: Python, numpy; How to best deal with possible 0d arrays
body: <p>Python numpy ndarrays are failing me!  Can I go back to Matlab??</p>
  
  <p>Let's say I have a function that is expecting a ndarray vector input.  I use the numpy.asarray function to force the inputs into the form I want, conveniently with no duplication for things that are already ndarrays.  However, if a scalar gets passed in, it is sometimes made into a 0d array instead of a 1d array, depending on exactly how it got passed in.  The 0d array causes issues, because I can index into it.</p>
  
  <p>First off, why can't I?  Say <code>x = np.array(1)</code>.  Then <code>x.size == 1</code>, so it should have a 0th element.  Why can't I do <code>x[0]</code> or <code>x[-1]</code>.  I get that it wants to be <em>like</em> a python int, but it should be improved over an int, and not purposely given the same limitations.</p>
  
  <p>Secondly, it would be awesome if the numpy asarray function had some optional input to force the output to always be at least a 1d array.  Then I could do something like <code>x = np.asarray(x, force_at_least_1d=True)</code>.</p>
  
  <p>However, the best option I could come up with is to check the ndim property, and if it's 0, then expand it to 1.  This just feels wrong to me.  Is there some other option that I'm missing?</p>
  
  <pre><code>import numpy as np
  
  def func(x, extra_check=True):
      r"""Meaningless Example Function for stackoverflow."""
      # force input to be ndarrays
      x = np.asarray(x)
  
      if x.size == 0:
          print('Don''t do anything.')
  
      # Extra test to deal with garbage 0D arrays so that they can be indexed by keep.
      # This test is really bothering me.  Is there a better way to make it unnecessary?
      if extra_check and (x.ndim == 0):
          x = x[np.newaxis]
  
      if x[0] &gt; 0 and x[-1] &gt; 5:
          print('Do something cool.')
      else:
          print('Do something less cool.')
  
  if __name__ == '__main__':
      # nominally intended use
      x1 = np.array([1, 2, 10])
      y1 = func(x1) # prints "Do something cool."
  
      # single item use
      x2 = x1[x1 == 2]
      y2 = func(x2) # prints "Do something less cool."
  
      # scalar single item use that works with extra check
      x3 = x1[1]
      y3 = func(x3) # prints "Do something less cool."
  
      # scalar single item that will fail without extra check
      x4 = x1[1]
      y4 = func(x4, extra_check=False) # raises IndexError
  </code></pre>
  
  <p>So my main question here is whether there's a better way than what I have.  And If not, do others agree that there should be?  I'm relatively new to Python, so I've never tried to contribute anything to the source yet, but presumably I can look for another question that explains that process to me.</p>
  
  <p>In case it matters, I'm on python v3.5.1 and numpy 1.9.3.  Thanks!</p>