简体   繁体   English

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

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

I would like to read data from redshift table and load it to dataframe and perform transformations.我想从红移表中读取数据并将其加载到 dataframe 并执行转换。 I used psycopg2 to connect to redshift and used pandas read_sql to query the table as below我使用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)

I see an Empty Dataframe but data exist when I query the database.我看到一个 Empty Dataframe 但是当我查询数据库时数据存在。 When I print the schema everything is non-null object .当我打印架构时,一切都是非空的 object

I parameterized the transfer_date as below and tried again.我将transfer_date参数化如下并再次尝试。 This time the whole data set is returned without any filter being applied.这次返回整个数据集而不应用任何过滤器。 Not sure where I'm missing.不知道我在哪里失踪。 I tried cast in the sql query itself but it returned an empty dataframe.我尝试在 sql 查询本身中进行转换,但它返回了一个空的 dataframe。 Any leads please.请提供任何线索。

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)

The data in redshift table is like below with datatype as varchar for col1 , col2 , col4 , col5 and date for transfer_date . 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

I think you're missing single quotes around the date, try with this:我认为您在日期周围缺少单引号,请尝试以下操作:

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

Sounds a bit weird, I haven't changed anything and it started working.听起来有点奇怪,我没有改变任何东西,它开始工作了。

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

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