numpy.genfromtxt flattens my data
<p>I have <code>row.csv</code></p>

<pre><code>1, 2, 3, 4, 5
</code></pre>

<p>and <code>column.csv</code></p>

<pre><code>1,
2,
3,
4,
5
</code></pre>

<p><code>numpy.genfromtxt("one-of-the-above.csv", delimiter=",")</code> reads them into the same array: <code>np.array([1,2,3,4,5])</code></p>

<p>but I would like to get distinct 2D arrays instead.</p>

<p>That is a "feature" of <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html" rel="nofollow"><code>genfromtxt</code></a>.  Before it returns the data that it read from the file, it uses <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html" rel="nofollow"><code>numpy.squeeze</code></a> to eliminate trivial dimensions.</p>

<p>If you use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html" rel="nofollow"><code>loadtxt</code></a> instead, you can use the argument <code>ndmin=2</code> to ensure that the result is always a two-dimensional array.</p>

<p>For example:</p>

<pre><code>In [15]: !cat one_row.csv
1, 2, 3, 4, 5

In [16]: !cat one_col.csv
1
2
3
4
5

In [17]: np.loadtxt('one_row.csv', delimiter=',', ndmin=2)
Out[17]: array([[ 1.,  2.,  3.,  4.,  5.]])

In [18]: np.loadtxt('one_col.csv', delimiter=',', ndmin=2)
Out[18]: 
array([[ 1.],
       [ 2.],
       [ 3.],
       [ 4.],
       [ 5.]])
</code></pre>

<p>If you would like to see this option added to <code>genfromtxt</code>, add a comment or a "thumbs up" to the issue that I created for it here: <a href="https://github.com/numpy/numpy/issues/4811" rel="nofollow">https://github.com/numpy/numpy/issues/4811</a></p>

python numpy