Pandas read_sql with parameters

7 浏览
0 Comments

Pandas read_sql with parameters

有没有在Pandas中使用SQL查询传递参数的示例?我特别是在使用SQLAlchemy引擎连接到PostgreSQL数据库。到目前为止,我发现以下方法可行:

df = psql.read_sql(('select "Timestamp","Value" from "MyTable" '
                     'where "Timestamp" BETWEEN %s AND %s'),
                   db,params=[datetime(2014,6,24,16,0),datetime(2014,6,24,17,0)],
                   index_col=['Timestamp'])

Pandas文档中提到`params`也可以作为字典传递,但我尝试过以下示例后似乎无法成功:

df = psql.read_sql(('select "Timestamp","Value" from "MyTable" '
                     'where "Timestamp" BETWEEN :dstart AND :dfinish'),
                   db,params={"dstart":datetime(2014,6,24,16,0),"dfinish":datetime(2014,6,24,17,0)},
                   index_col=['Timestamp'])

在Pandas中运行这些类型的查询的推荐方法是什么?

0