Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

python - Unable to query a local variable in 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'

In pandas 0.13.1, I could simply use:

fills.query('Symbol==my_symbol')

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')

but this returned an error

KeyError: '__pd_eval_local_my_symbol'

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...