difference between pandas read sql query and read sql table

Oussama Jabri :

Is there a difference in relation to time execution between this two commands :

import pandas as pd

df=pd.read_sql_query('SELECT * FROM TABLE',conn)
df=pd.read_sql_table(TABLE, conn)

Thank you for your help

MaxU :

I don't think you will notice this difference.

Here is a source code for both functions:

In [398]: pd.read_sql_query??
Signature: pd.read_sql_query(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, chunksize=None)
Source:
def read_sql_query(sql, con, index_col=None, coerce_float=True, params=None,
                   parse_dates=None, chunksize=None):
    pandas_sql = pandasSQL_builder(con)
    return pandas_sql.read_query(
        sql, index_col=index_col, params=params, coerce_float=coerce_float,
        parse_dates=parse_dates, chunksize=chunksize)

and

In [399]: pd.read_sql_table??
Signature: pd.read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=True, parse_dates=None, columns=None, chunksize=None
)
Source:
def read_sql_table(table_name, con, schema=None, index_col=None,
                   coerce_float=True, parse_dates=None, columns=None,
                   chunksize=None):
    con = _engine_builder(con)
    if not _is_sqlalchemy_connectable(con):
        raise NotImplementedError("read_sql_table only supported for "
                                  "SQLAlchemy connectable.")
    import sqlalchemy
    from sqlalchemy.schema import MetaData
    meta = MetaData(con, schema=schema)
    try:
        meta.reflect(only=[table_name], views=True)
    except sqlalchemy.exc.InvalidRequestError:
        raise ValueError("Table %s not found" % table_name)

    pandas_sql = SQLDatabase(con, meta=meta)
    table = pandas_sql.read_table(
        table_name, index_col=index_col, coerce_float=coerce_float,
        parse_dates=parse_dates, columns=columns, chunksize=chunksize)

    if table is not None:
        return table
    else:
        raise ValueError("Table %s not found" % table_name, con)

NOTE: i have iintentionally cut off docstrings...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can pandas.read_sql_query() query a TEMP table?

Is there a difference between read_table and read_csv in pandas?

PySpark equivalent of pandas read_sql_query

Pandas read_sql_query with WHERE condition

Pandas read_sql query with multiple selects

Pandas read_sql_query with comprehension in params

What's the difference between spark.sql() and spark.read.format("jdbc").option("query", "")?

Read SQL table into SparklyR

how to delete the only the rows in postgres but not to drop table using pandas read_sql_query method?

Read SQL query into pandas dataframe and replace string in query

read.sql_query works, read sql_table doesn't

How to pass table name in read_sql_query

Slow Response on Read/Delete Query on a Table with Composite Key - SQL Server

Equivalent of pandas read_sql_query for numpy array?

How do I read a downloades sql query on pandas

Python Pandas read_sql_query “'NoneType' object is not iterable” error

How to read sql query to pandas dataframe / python / django

how to pass columname as params with pandas read_sql_query function

pandas - cdecimal.ConversionSyntax invalidOperation on read_sql_query()

Binding list to params in Pandas read_sql_query with other params

Pandas read_sql_query using multiple AND statements

Pandas read_sql_query with parameters for a string with no quotes

Pandas read_sql_query turning float number to int

Pandas read_sql Challenging syntax for postgres query

pandas read_sql. How to query with where clause of date field

Error fromatting parameter for Pandas read_sql_query

How to use params from pandas.read_sql to import data with Python pandas from SQLite table between dates

pandas.read_sql Read uncommitted with SQLAlchemy

SQL Query - Finding a difference between 2 values in same table