title: Numpy: Uniform way of retrieving `dtype`
body: <p>If I have a numpy array <code>x</code>, I can get its data type by using <code>dtype</code> like this:</p>
  
  <pre><code>t = x.dtype
  </code></pre>
  
  <p>However, that obviously won't work for things like lists. I wonder if there is a standard way of retrieving types for lists and numpy arrays. In the case of lists, I guess this would mean the largest type which fits all of the data. For instance, if </p>
  
  <pre><code>x = [ 1, 2.2 ]
  </code></pre>
  
  <p>I would want such a method to return <code>float</code>, or better yet <code>numpy.float64</code>. </p>
  
  <p>Intuitively, I thought that this was the purpose of the <code>numpy.dtype</code> method. However, that is not the case. That method is used to <em>create</em> a type, not <em>extract</em> a type. </p>
  
  <p>The only method that I know of getting a type is to wrap whatever object is passed in with a numpy array, and then get the <code>dtype</code>:</p>
  
  <pre><code>def dtype(x):
      return numpy.asarray(x).dtype
  </code></pre>
  
  <p>The issue with this approach, however, is that it will copy the array if it is not already a numpy array. In this circumstance, that is extremely heavy for such a simple operation. </p>
  
  <p>So is there a <code>numpy</code> method that I can use which won't require me to do any list copies? </p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>I am designing a library for doing some geometric manipulations... Conversions between rotation matrices, rotation vectors, quaternions, euler angles, etc. </p>
  
  <p>It can easily happen that the user is simply working with a single rotation vector (which has 3 elements). In that case, they might write something like </p>
  
  <pre><code>q = vectorToQuaternion([ .1, 0, 0 ])
  </code></pre>
  
  <p>In this case, I would want the output quaternion to be a numpy array of type <code>numpy.float64</code>. However, sometimes to speed up the calculations, the user might want to use a numpy array of float32's:</p>
  
  <pre><code>q = vectorToQuaternion(numpy.float32([ .1, 0, 0 ]))
  </code></pre>
  
  <p>In which case, I think it is natural to expect that the output is the same type. </p>
  
  <p>The issue is that I cannot use the <code>zeros_like</code> function (or <code>empty_like</code>, etc) because a quaternion has 4 components, while a vector has 3. So internally, I have to do something like </p>
  
  <pre><code>def vectorToQuaternion(v):
      q = empty( (4,), dtype = asarray(v).dtype )
      ...
  </code></pre>
  
  <p>If there was a way of using <code>empty_like</code> which extracts all of the properties of the input, but lets me specify the shape of the output, then that would be the ideal function for me. However, to my knowledge, you cannot specify the shape in the call to <code>empty_like</code>.</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>Here are some gists for the class I am talking about, and a test class (so that you can see how I intend to use it).</p>
  
  <p>Class: <a href="https://gist.github.com/mholzel/c3af45562a56f2210270d9d1f292943a" rel="nofollow noreferrer">https://gist.github.com/mholzel/c3af45562a56f2210270d9d1f292943a</a></p>
  
  <p>Tests: <a href="https://gist.github.com/mholzel/1d59eecf1e77f21be7b8aadb37cc67f2" rel="nofollow noreferrer">https://gist.github.com/mholzel/1d59eecf1e77f21be7b8aadb37cc67f2</a> </p>