首页 > 解决方案 > 将 pandas.read_sql_query 与 PostgreSQL 一起使用会返回 Empty DataFrame

问题描述

这是代码:

sql = "select to_char(f_date at time zone 'GMT-8', 'YYYY-MM-DD 
       HH24:MI:SS')  as f_date, f_open, f_close, f_high, f_low, f_volume 
       from choice_kwindminutefinancehistory 
       INNER JOIN choice_financecode 
             ON choice_kwindminutefinancehistory.f_code_id=choice_financecode.id 
       where choice_financecode.f_code = '" + stock + "' 
             AND f_date >='" + start_date + "' and f_date<='" + end_date + "'"

df = pd.read_sql_query(sql, con=self.engine)

这工作得很好,我从查询中得到了一个数据框,但是当我更改 SQL(添加了两个内部连接)时,如下所示:

sql = "select to_char(kwm.f_date at time zone 'GMT-8', 'YYYY-MM-DD 
       HH24:MI:SS') as f_date , kwm.f_open as f_open, kwm.f_close as f_close, 
       kwm.f_high as f_high, kwm.f_low as f_low, kwm.f_volume as f_volume, 
       dfh.f_date as pre_date, dfh.f_open as pre_open, dfh.f_close as pre_close, 
       dfh.f_high as pre_high, dfh.f_low as pre_low, dfh2.f_volume as today_volumn 
       from choice_kwindminutefinancehistory as kwm 
       INNER JOIN choice_financecode as fc ON kwm.f_code_id=fc.id 
       INNER JOIN choice_dailyfinancehistory as dfh ON kwm.f_code_id = dfh.f_code_id 
             and dfh.f_date = '" + pre_date + "' 
       INNER JOIN choice_dailyfinancehistory as dfh2 ON kwm.f_code_id = dfh2.f_code_id 
             and dfh2.f_date = '" + start_date + "' 
       where fc.f_code = '" + stock + "' AND kwm.f_date >= '" + start_date + "' 
       and kwm.f_date<='" + end_date + "'"

我有一个空数据框。然后我在屏幕上打印SQL,复制并粘贴到DataGrip,SQL 工作,它可以返回行,但是在程序中,使用read_sql_query()它返回空,我不知道为什么。

标签: pythonpostgresqlpandas

解决方案


推荐阅读