Note that there are some explanatory texts on larger screens.

plurals
  1. POPYODBC to Pandas - DataFrame not working - Shape of passed values is (x,y), indices imply (w,z)
    text
    copied!<p>I used pyodbc with python before but now I have installed it on a new machine ( win 8 64 bit, Python 2.7 64 bit, PythonXY with Spyder). </p> <p>Before I used to (at the bottom you can find more real examples):</p> <pre><code>columns = [column[0] for column in cursor.description] temp = cursor.fetchall() data = pandas.DataFrame(temp,columns=columns) </code></pre> <p>and it would work fine. Now it seems like DataFrame is not able to convert from the data fetched from the cursor anymore. It returns:</p> <p>Shape of passed values is (x,y), indices imply (w,z)</p> <p>I kind of see where the issue is. Basically, imagine I fetch only one row. Then DataFrame would like to shape it (1,1), one element only. While I would like to have (1,X) where X is the length of the list. </p> <p>I am not sure why the behavior changed. Maybe it is the Pandas version I have, or the pyodbc, but updating is problematic. I tried to update some modules but it screws up everything, any method I use (binaries--for the right machine/installation--pip install, easy-install,anything! etc.. which is very frustrating indeed. I would probably avoid Win 8 64 bit from now on for Python).</p> <p>Real examples:</p> <pre><code>sql = 'Select * form TABLE' cursor.execute(sql) columns = [column[0] for column in cursor.description] data = cursor.fetchall() con.close() results = DataFrame(data, columns=columns) </code></pre> <p>Returns: <em>*</em> ValueError: Shape of passed values is (1, 1540), indices imply (51, 1540)</p> <p>Notice that:</p> <pre><code>ipdb&gt; type(data) &lt;type 'list'&gt; ipdb&gt; np.shape(data) (1540, 51) ipdb&gt; type(data[0]) &lt;type 'pyodbc.Row'&gt; </code></pre> <p>Now, for example, if we do:</p> <pre><code>ipdb&gt; DataFrame([1,2,3],columns=['a','b','c']) </code></pre> <p><em>*</em> ValueError: Shape of passed values is (1, 3), indices imply (3, 3)</p> <p>and if we do:</p> <pre><code>ipdb&gt; DataFrame([[1,2,3]],columns=['a','b','c']) </code></pre> <p>a b c 0 1 2 3</p> <p>However, even trying:</p> <pre><code>ipdb&gt; DataFrame([data[0]], columns=columns) *** ValueError: Shape of passed values is (1, 1), indices imply (51, 1) </code></pre> <p>or</p> <pre><code>ipdb&gt; DataFrame(data[0], columns=columns) *** PandasError: DataFrame constructor not properly called! </code></pre> <p>Please help :) Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload