【问题标题】:Is there a way to write a pandas SQL query across multiple lines with comments?有没有办法跨多行编写带有注释的 pandas SQL 查询?
【发布时间】:2016-05-10 09:19:42
【问题描述】:

在编写正则表达式时,可以跨多行编写表达式并包括注释,然后在传递编译版本之前使用re.VERBOSE 选项编译表达式。我想用pandas.read_sql_query 做类似的事情。

例如,而不是:

result = pd.read_sql_query('select a.gvkey, a.tic, a.datadate as fyearend, year(a.datadate) as year, month(a.datadate) as fyrc, b.datadate, month(b.datadate) as month, b.trt1m from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) where a.TIC = "IBM" and a.datafmt = "STD" and a.consol="C" and a.indfmt = "INDL" and year(a.datadate)>1980', engine)

我想写这样的东西:

q = """select a.gvkey, 
    a.tic,                      #COMMENTS
    a.datadate as fyearend,     #COMMENTS
    year(a.datadate) as year,   #COMMENTS
    month(a.datadate) as fyrc, b.datadate, 
    month(b.datadate) as month, 
    b.trt1m 
    from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) 
    where a.TIC = "IBM" 
        and a.datafmt = "STD" 
        and a.consol="C" 
        and a.indfmt = "INDL" 
        and year(a.datadate)>1980
"""

result = p.read_sql_query(q ,engine)

我的问题与this有关将python命令拆分为多行的问题有关,但我想在查询中添加cmets。

正如我所提到的,我希望在 pandas/SQL 案例中执行的操作类似于在 re.VERBOSE 的正则表达式案例中可以执行的操作。这是一个正则表达式的例子:

pattern = r'''\s(shares?| #COMMENTS
            warrants?|       #COMMENTS
            stock|           #AND SO ON...
            (non)?vest(ed)?
            )\b             
            '''
crit = re.compile(pattern_nopt, re.VERBOSE)
match=re.search(crit, string)

这将使查询更具可读性,我发现在与共同作者共享代码时详尽地注释查询很重要。

【问题讨论】:

    标签: python sql pandas


    【解决方案1】:

    是的,它会起作用,但您必须使用正确的 comment delimiter for SQLite
    -- 用于内联注释
    /* foo.. */(如在 C 中)用于多行注释

    所以它看起来像:

    q = """select a.gvkey, 
        a.tic,                      -- COMMENTS
        a.datadate as fyearend,     -- COMMENTS
        year(a.datadate) as year,   /* Another very long
        and multi-lines comment... */
        month(a.datadate) as fyrc, b.datadate, 
        month(b.datadate) as month, 
        b.trt1m from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) 
        where a.TIC = "IBM" 
            and a.datafmt = "STD" 
            and a.consol="C" 
            and a.indfmt = "INDL" 
            and year(a.datadate)>1980
    """
    
    result = p.read_sql_query(q, conn)
    

    【讨论】:

    • mgc,谢谢!这在我使用多行注释(/* foo */)时有效,但在我使用内联注释(--)时无效。您对为什么会这样有任何想法吗?
    • @ArthurMorris 不客气!我真的不知道,我用内联分隔符先测试(成功)。您确定以 -- 开头的评论以 end-of-line 结尾吗?或者你有什么错误信息?
    • 评论似乎以换行结束,但我可能错了。我得到的错误是:Syntax error, expecting one of the following: ;, !, !!, &, (, *, **, +, -, '.', /, <, <=, <>, =, >, >=, AND, EQ, EQT, EXCEPT, GE, GET, GROUP, GT, GTT, HAVING, INTERSECT, LE, LET, LT, LTT -2147295222 ERROR 22-322
    • 再次感谢您的回答!我会自己进一步调试。
    • 事实证明,我的问题是由于我正在查询的数据库使用 SAS SQL,因此-- cmets 未被识别为 cmets。我想教训是 SQL 查询中的注释格式是由处理查询的系统决定的,而不是由 Python 决定的。
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2021-09-24
    • 2019-12-25
    • 1970-01-01
    • 2019-05-25
    相关资源
    最近更新 更多