operator does not exist: timestamp with time zone >= character varying / pandas 链接 psycopg2保存数据问题

sql 取 7天内的数据:

and create_time >= cast(CURRENT_DATE - 7 as varchar)

出现:

ERROR:  operator does not exist: timestamp with time zone >= character varying

LINE 5:  and create_time >= cast(CURRENT_DATE - 7 as varchar)

HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

异常原因 是不同类型 的 数据进行了对比,需要 把 对比的信息 转换时间格式

and create_time >= cast(CURRENT_DATE - 7 as varchar)::TIMESTAMP

用 pandas 使用 sql 读取数据时 出现

"Excel does not support datetimes with "
ValueError: Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.

是因为 to_excel 的 时间格式有问题 使用 to_csv 不会有时间报错问题

# 增加这行代码
data1['create_time'] = data1['create_time'].dt.tz_localize(None)
import pandas as pd
import psycopg2
# 建立数据库连接
con = psycopg2.connect(database="***", user="***", password="***", host="***", port="5432") 
sql_command = """
select tag, project, create_time, job_url, node_name
    from public.query_build_case_result
    where node_name = '***-053'
    and create_time >= cast(CURRENT_DATE - 60 as varchar)::TIMESTAMP
	"""
"""
data1 = pd.read_sql(sql_command, con)
data1['create_time'] = data1['create_time'].dt.tz_localize(None)  # 增加这行不会报错
file_path = f'file_0531.xlsx'
data1.to_excel(excel_writer=file_path, sheet_name='sheet_1')
# data1.to_csv(file_path.replace('.xlsx', '.csv'))  # 使用这个 可以不用增加.dt.tz_localize(None) 变形操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值