看起来你的query._current_rows 属性是一个pandas DataFrame,所以当你尝试运行query.current_rows 时,总会引发ValueError,不管ResultSet 是否为空。
从ResultSet 的Cassandra Driver docs 中,我们看到current_rows 函数寻找_current_rows 属性的存在:
@property
def current_rows(self):
"""
The list of current page rows. May be empty if the result was empty,
or this is the last page.
"""
return self._current_rows or []
如果上面的self._current_rows 是一个pandas DataFrame,这将总是返回一个ValueError。例如:
>>> data = {'col1': [1, 2], 'col2': [3, 4]}
>>> df = pd.DataFrame(data)
>>> df
col1 col2
0 1 3
1 2 4
>>> df or []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/src/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
>>> df = pd.DataFrame()
>>> df or []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/src/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pandas/core/generic.py", line 1573, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
因此,要检查 ResultSet 中的 pandas DataFrame 是否包含数据,您可以执行以下操作:
if not query._current_rows.empty:
print 'y'
else:
print 'n'
(注意:我不知道您的session.row_factory 是什么样的,但我假设它正在从Cassandra 返回的行创建一个pandas DataFrame,类似于this 答案)