运行NOSE测试时PANDAS read_sql出现错误 - 无法解析字符串

时间:2018-02-19 22:29:17

标签: python pandas sqlite nose

您是否看过这个pandas sqlite错误? ArgumentError("Could not parse rfc1738 URL from string '/')?它只发生在NOSE测试中。

关于SO的其他问题,错误似乎与sqlite有关。但我无法告诉我,因为没有其他人像我一样使用简单的sqlite3数据库。

在我的单元测试中,我调用了一个通过pandas read_sql从sqlite3数据库读取的类方法。它在任何python会话以及Jupyter笔记本中都能很好地工作。但由于某些原因,当我通过鼻子测试运行代码时,鼻子告诉我有一个参数错误。我无法重现它。

我已确认数据库工作正常。正如我所提到的,pd.read_sql在任何其他设置中都能完美运行。

在方法定义中,我正在进行以下阅读,

# get data
div_query = 'SELECT * FROM Dividends WHERE Symbol == "{stock}"'.format(stock = symbol)
div_history = pd.read_sql(div_query, con=dbcnx)

在NOSE测试中,

def test_shiftBeforeDividend():
    # here is where the err occurs
    result = filters.shiftBeforeDividend('DUK') 
    # result now equals ArgumentError("Could not parse rfc1738 URL from string '/'")

def setup():
    try:    
        db = 'mydb.db'
        test_class.connectToDB(db)
        return test_class

    except Exception as e:
        return e

def teardown():
    try:
        filters.closeDBConnection(self.dbcnx[0])

    except Exception as e:
        return e

# startup
filters = setup()

关于如何消除这个问题的任何想法?

1 个答案:

答案 0 :(得分:0)

经过一些头发拉动后发现错误是一个简单的错位' /'在db文件路径中。因此,sqlite无法连接到数据库,并且所有对数据库的调用都会导致错误。在我阅读类似主题的所有帮助主题中,似乎该错误总是由不正确的DB引用(即文件路径)引起。因此,如果您遇到此问题,请在尝试对数据库执行操作之前确保文件路径存在且正确无误。