how to use pandas.io.sql.read_frame?

Discussion in 'App Development' started by zedDoubleNaught, May 13, 2012.

  1. I have python 2.7, and pandas 0.7.0, and I'm trying to query MySQL. When I do the query, the records in python are a bunch of tuples. I'm trying to convert them to a DataFrame, but not having much luck.

    I've tried with pandas.io.sql.read_frame(), which is supposed to return the result set as a DataFrame. However, when I run it, it gives me an error that it got a type tuple when it expected a list.

    Another option would be iterating through the results a few times, to get the column names, have several Series, then put those Series into a DataFrame. But, this seems like it would be the slow way.

    Code:
    import pandas as pd
    import pd.io.sql as psql
    import MySQLdb as db

    conn = db.Connection( // connection info)
    query = 'select * from crappo'
    df = psql.read_frame(query, conn, 'ID') // tried with and without index column

    this results in:

    TypeError: Argument 'rows' has incorrect type (expected list, got tuple)

    I've looked for documentation, but could not find any. Any ideas, is there something simple I've missed? thanks
     
  2. Yes, that this is elitetrader not stackexchange. You're welcome.
     
  3. If anyone was wondering about the original question, I found a way to do this - use MySQLdb to get a cursor (instead of pandas), fetch all into a tuple, then cast that as a list when creating the new DataFrame:

    my_DF = DataFrame(data=list(tupleFromCursor), columns=columnsFromCursor)

    list(tupleFromCursor) got it to work.
     
  4. Thanks. Useful to know.