Error setting dtypes of an array
<p>I was attempting to make a 1x5 numpy array with the following code</p>

<pre><code>testArray = np.array([19010913, "Hershey", "Bar", "Birthday", 12.34])
</code></pre>

<p>but encountered the unwanted result that</p>

<pre><code>testArray.dtype
dtype("&lt;U8")
</code></pre>

<p>I want each column to be a specific data type, so I attempted to input this</p>

<pre><code>testArray = np.array([19010913, "Hershey", "Bar", "Birthday", 12.34],
    dtype=[('f0','&lt;i8'),('f1','&lt;U64'),('f2','&lt;U64'),('f3','&lt;U64'),('f4','&lt;f10')] )
</code></pre>

<p>but got the error</p>

<pre><code>/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py:1:
DeprecationWarning: Specified size is invalid for this data type.
Size will be ignored in NumPy 1.7 but may throw an exception in
future versions. if __name__ == '__main__':
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-11-d2c44d88c8a5&gt; in &lt;module&gt;()
----&gt; 1 testArray = np.array([19840913, "Hershey", "Bar",
"Birthday", 64.25], dtype=[('f0','&lt;i8'),('f1','&lt;U64'),('f2','&lt;U64'), ('f3','&lt;U64'),('f4','&lt;f10')] )

TypeError: 'int' does not support the buffer interface
</code></pre>

<p>First off, I am not sure if <code>f10</code> is something known. </p>

<p>Note that structured arrays need to be defined as "list of tuples". Try the following:</p>

<pre><code>testArray = np.array([(19010913, "Hershey", "Bar", "Birthday", 12.34)],
dtype=[('f0','&lt;i8'),('f1','&lt;U64'),('f2','&lt;U64'),('f3','&lt;U64'),('f4','&lt;f8')])
</code></pre>

<p>See also <a href="http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html" rel="nofollow">this</a> and <a href="http://docs.scipy.org/doc/numpy-1.10.1/user/basics.rec.html" rel="nofollow">this</a> for different ways of defining <code>np.dtype</code>s and structured arrays.</p>

<p>Edit:</p>

<p>For multiple rows in the same structure, define each row of your array as a separate tuple in the list.</p>

<pre><code>dt = np.dtype([('f0','&lt;i8'),('f1','&lt;U64'),('f2','&lt;U64'),('f3','&lt;Uâ64'),('f4','&lt;f8')])

testArray = np.array([(19010913, "Hershey", "Bar", "Birthday", 12.34), (123, "a", "b", "c", 56.78)], dtype=dt)
</code></pre>

python arrays numpy