有没有办法用注释编写跨多行的 pandas SQL 查询?

2024-01-07

编写正则表达式时,可以跨多行编写表达式并包含注释,然后使用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 https://stackoverflow.com/questions/33944522/is-it-possible-to-split-a-sequence-of-pandas-commands-across-multiple-lines关于将 python 命令拆分为多行的问题,但我想在查询中添加注释。

正如我所提到的,我想要在 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)

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


是的,它会起作用,但你必须使用正确的方法SQLite 的注释分隔符 https://www.sqlite.org/lang_comment.html :
--对于内嵌评论
/* 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有办法用注释编写跨多行的 pandas SQL 查询? 的相关文章

随机推荐