Read Teradata query into Pandas

2020-07-25 03:39发布

问题:

Has anyone found a way to read a Teradata query into a Pandas dataframe? It looks like SQLAlchemy does not have a Teradata dialect.

http://docs.sqlalchemy.org/en/latest/dialects/

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html

回答1:

I did it using read_sql . Below id the code snip :

def dqm() :
    conn_rw = create_connection()
    dataframes = []
    srcfile = open('srcqueries.sql', 'rU').read()
    querylist = srcfile.split(';')
    querylist.pop()
    for query in querylist :
        dataframes.append(pd.read_sql(query, conn_rw))
    close_connection(conn_rw)
    return dataframes,querylist

You can create connection as below :

    def create_connection():
        conn = pyodbc.connect("DRIVER=Teradata;DBCNAME=tddb;UID=uid;PWD=pwd;QUIETMODE=YES", autocommit=True,unicode_results=True)
        return conn

You can check complete code here : GitHub Link Let me know if this answers your query .



回答2:

You can use slqalchemy but you will need to install sqlalchemy-teradata too. You can do that via PIP

pip install sqlachemy-teradata

The rest of the code remains the same :)

from sqlalchemy import create_engine
import pandas as pd

user, pasw, host = 'username','userpass', 'hostname'

# connect
td_engine = create_engine('teradata://{}:{}@{}:22/'.format(user,pasw,hostname))

# execute sql
query = 'select * from dbc.usersV'
result = td_engine.execute(query)

#To read your query to Pandas
df = pd.read_sql(query,td_engine)