You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pandasql运行SQL报OperationalError near "("语法错误问题咨询

报错原因

pandasql底层基于SQLite运行SQL语句,row_number() over() 这类窗口函数是SQLite 3.25.0版本才新增的特性,低于该版本的SQLite无法识别OVER子句相关语法,就会抛出near "(": syntax error的报错。你删除row_number后括号的操作没有解决根本问题,低版本SQLite依然识别不了OVER关键字,所以报错依然存在。

解决方案

优先推荐使用无需窗口函数的兼容写法,不需要调整环境依赖,实现效果和原写法完全一致:

  • 调整SQL语句,移除窗口函数相关逻辑,保留分组统计逻辑并按cumSum排序:
query = '''
select min(rowLoc) as SegmentStart
, max(rowLoc) - min(rowLoc) as Height
from df_lineLocations
where line = 0
--and CumSum !=0
group by cumSum
order by cumSum
'''
  • 执行查询后,通过pandas直接生成序号列即可实现原row_number() over (order by cumSum)的效果:
df_SegmentLocations  = ps.sqldf(query, locals())
# 生成从1开始的连续序号
df_SegmentLocations['SegmentOrder'] = range(1, len(df_SegmentLocations) + 1)
# 调整列顺序匹配原需求
df_SegmentLocations = df_SegmentLocations[['SegmentOrder', 'SegmentStart', 'Height']]

如果希望直接使用窗口函数语法,可以升级环境内的SQLite版本到3.25.0及以上,升级完成后可通过执行如下代码验证版本:

import sqlite3
print(sqlite3.sqlite_version)

输出结果大于等于3.25.0即可正常运行原SQL语句。

内容的提问来源于stack exchange,提问作者Mohamed Amine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 01:42:04