繁体   English   中英

使用 psycopg2 从红移查询时为空 pandas dataframe

[英]Empty pandas dataframe when querying from redshift using psycopg2

我想从红移表中读取数据并将其加载到 dataframe 并执行转换。 我使用psycopg2连接到 redshift 并使用 pandas read_sql查询表如下

con = psycopg2.connect("dbname=sales host=redshifttest-xyz.cooqucvshoum.us-west-2.redshift.amazonaws.com port=5439 user=master password=secret")
cur = con.cursor()
sql = "select * from dtw.rpt_account_transfer_hist where transfer_date>=2020-07-01;"
df  = pd.read_sql(sql, con)

我看到一个 Empty Dataframe 但是当我查询数据库时数据存在。 当我打印架构时,一切都是非空的 object

我将transfer_date参数化如下并再次尝试。 这次返回整个数据集而不应用任何过滤器。 不知道我在哪里失踪。 我尝试在 sql 查询本身中进行转换,但它返回了一个空的 dataframe。 请提供任何线索。

curr_dt = datetime.strftime(datetime.now() - timedelta(3), '%Y-%m-%d')
sql = "select * from dtw.rpt_account_transfer_hist where transfer_date>=" +str(curr_dt)+";"
df  = pd.read_sql(sql, con)

redshift 表中的数据如下所示,其中col1col2col4col5的数据类型为varchartransfer_date的数据类型为date

col1    col2   transfer_date col4    col5
6052148 670018  2020-07-13  640033  6052148
5260969 640737  2020-07-11  640033  5260969
4778065 610050  2020-07-11  610017  4778065
7942224 690020  2020-07-11  690032  7942224
5260969 640737  2020-07-10  640033  5260969
4778065 610050  2020-07-10  610017  4778065
7942224 690020  2020-07-10  690032  7942224
5073022 640601  2020-07-09  640679  5073022
0309991 640601  2020-07-09  640729  0309991

我认为您在日期周围缺少单引号,请尝试以下操作:

sql = "select * from dtw.rpt_account_transfer_hist where transfer_date>='2020-07-01';"

听起来有点奇怪,我没有改变任何东西,它开始工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM