是否有人找到了将Teradata查询读入Pandas数据帧的方法?看起来SQLAlchemy没有Teradata方言。
http://docs.sqlalchemy.org/en/latest/dialects/
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html
答案 0 :(得分:2)
我是使用// flag for non-missing non-zero assets in 1993
generate flag = year==1993 & at<. & at>0
by gvkey (year), sort: egen tokeep = max(flag)
keep if tokeep
drop flag tokeep
完成的。下面是代码片段:
read_sql
您可以按如下方式创建连接:
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
您可以在此处查看完整代码:GitHub Link 如果这回答了您的问题,请告诉我。
答案 1 :(得分:2)
您可以使用slqalchemy,但您也需要安装sqlalchemy-teradata。你可以通过PIP
来做到这一点pip install sqlachemy-teradata
其余代码保持不变:)
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)