无法在Pandas 0.14.0中查询局部变量 [英] Unable to query a local variable in pandas 0.14.0

查看:61
本文介绍了无法在Pandas 0.14.0中查询局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以查询一个明确的值:

I can query an explicit value:

fills.query('Symbol=="BUD US"')

现在我要查询一个变量:

Now I want to query a variable:

In [40]: my_symbol
Out[40]: 'BUD US'

在熊猫0.13.1中,我可以简单地使用:

In pandas 0.13.1, I could simply use:

fills.query('Symbol==my_symbol')

在熊猫0.14.0中不再允许这样做,因为本地变量必须明确引用.所以我尝试了

This is no longer permitted in pandas 0.14.0 because local variables must be referred to explicitly. So I tried

fills.query('Symbol==@my_symbol')

但这返回了错误

KeyError: '__pd_eval_local_my_symbol'

我能够将其他一些代码更改为使用显式的局部变量,但是这一点不会用.有什么想法吗? dtype是object,如果有帮助的话.

I was able to change some of my other code to use explicit local variables, but this one just will not take. Any thoughts? The dtype is object, if that helps.

推荐答案

似乎是0.14中的一个错误,特别是在字符串上(例如,在使用ints时).我在这里提出了一个问题: https://github.com/pydata/pandas/issues/7300.
作为目前的解决方法,您可以将其包装在列表中:

Seems a bug in 0.14, specifically with strings (works eg with ints). I filed an issue here: https://github.com/pydata/pandas/issues/7300.
As a workaround for now, you can wrap it in a list:

In [40]: fills
Out[40]: 
    Price  Symbol
0  109.70  BUD US
1  109.72  BUD US
2  183.30  IBM US
3  183.35  IBM US

In [41]: my_symbol = ['BUD US']

In [42]: fills.query('Symbol==@my_symbol')
Out[42]: 
    Price  Symbol
0  109.70  BUD US
1  109.72  BUD US

这篇关于无法在Pandas 0.14.0中查询局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆