在python 2中使用sqldf,收到错误

时间:2017-06-03 05:17:51

标签: python python-2.7

我的代码如下所示:

pip install pandasql

import pandasql as pdsql

str1="select * from data5 limit 5;" 


df11=pdsql.sqldf(str1)

但是,我收到此错误消息:

Error:   

 AttributeError                            Traceback (most recent call last)
    <ipython-input-55-316d12745a46> in <module>()
    ----> 1 df11=pdsql.sqldf(str1)


C:\py\lib\site-packages\pandasql\sqldf.pyc in sqldf(query, env, db_uri)
    154     >>> sqldf("select avg(x) from df;", locals())
    155     """
--> 156     return PandaSQL(db_uri)(query, env)

C:\py\lib\site-packages\pandasql\sqldf.pyc in __call__(self, query, env)
     45         """
     46         if env is None:
---> 47             env = get_outer_frame_variables()
     48 
     49         with self.conn as conn:

C:\py\lib\site-packages\pandasql\sqldf.pyc in get_outer_frame_variables()
     95     cur_filename = inspect.getframeinfo(inspect.currentframe()).filename
     96     outer_frame = next(f
---> 97                        for f in inspect.getouterframes(inspect.currentframe())
     98                        if f.filename != cur_filename)
     99     variables = {}

C:\py\lib\site-packages\pandasql\sqldf.pyc in <genexpr>((f,))
     96     outer_frame = next(f
     97                        for f in inspect.getouterframes(inspect.currentframe())
---> 98                        if f.filename != cur_filename)
     99     variables = {}
    100     variables.update(outer_frame.frame.f_globals)

AttributeError: 'tuple' object has no attribute 'filename'

1 个答案:

答案 0 :(得分:1)

试试这个:

import pandasql as pdsql

pysqldf = lambda q: sqldf(q, globals()) res = pysqldf(your_query)

我希望它有所帮助。