Closed dandye closed 12 years ago
Creating a DataFrame from a list of tuples should work fine. See example below. Could it be that rows is not a list holding tuples but maybe a tuple holding tuples? I am not an expert on db-api`s, i would expect rows to be a list, bit it does not seem to be the case based on the traceback you get.
In [14]: pandas.__version__
Out[14]: '0.7.3' # my estimated guess on your pandas version, cfr. last traceback.
In [15]: df = pandas.DataFrame([(0, 'foo', 10.0), (1, 'bar')])
In [16]: df
Out[16]:
0 1 2
0 0 foo 10
1 1 bar NaN
Iodagro, You are correct: the problem isn't the list of tuples needing to be a list of lists but rather with a tuple of tuples:
>>> rows = cursor.fetchall()
>>> type(rows)
<type 'tuple'>
>>> pandas.__version__
'0.7.3'
Had a look in the code and this has been fixed. In the latest release sql.read_frame()
can handle db-api that returns tuple instead of a list for cursor.fetchall()
.
The MySQLdb driver returns rows as tuples instead of lists and Pandas isn't happy about it:
I confirmed that Listifying the rows fixes the issue:
Also, calling the DataFrame constructor with the Tuple rows raises an error that isn't very informative:
>>> df = DataFrame(rows,columns=colnames)
Traceback (most recent call last): File "