在pandas.DataFrame.query()表达式中使用负数

Mri*_*uri 7 python pandas

我正在尝试使用pandas.DataFrame.query()函数,如下所示:

expression_string = 'ColumnName<(-1000)'
output_dataframe = dataframe.query(expression_string)
Run Code Online (Sandbox Code Playgroud)

代码使用正数,但是当负数传递给字符串时,如上所述,它返回以下错误:

AttributeError: 'UnaryOp' object has no attribute 'value'
Run Code Online (Sandbox Code Playgroud)

有关如何在DataFrame query()表达式中使用负数的任何建议?谢谢!!

jpp*_*jpp 5

pandas我可以在v0.20.3 上使用特定数据类型重现此错误;例如,np.float32。解决方法是显式转换为float.

这是一个已知的错误:DataFrame.eval 错误与 AttributeError: 'UnaryOp'

df = pd.DataFrame({'A': [-3.0, -2.5, -1.5, 3.0]}, dtype=np.float32)

x = 'A>(-1)'

# AttributeError:
res = df.query(x)

# Success:
df['A'] = df['A'].astype(float)
res = df.query(x)
Run Code Online (Sandbox Code Playgroud)