Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using negative numbers in pandas.DataFrame.query() expression

Tags:

python

pandas

I am trying to use the pandas.DataFrame.query() function as follows:

expression_string = 'ColumnName<(-1000)'
output_dataframe = dataframe.query(expression_string)

The code works with positive numbers, but when negative numbers are passed to the string, as above, it returns the following error:

AttributeError: 'UnaryOp' object has no attribute 'value'

Any suggestions on how to use negative numbers in DataFrame query() expressions? Thank you!!

like image 442
Mridula Gunturi Avatar asked May 17 '18 21:05

Mridula Gunturi


People also ask

What is query () in Python?

The query() method allows you to query the DataFrame. The query() method takes a query expression as a string parameter, which has to evaluate to either True of False. It returns the DataFrame where the result is True according to the query expression.

How do you convert a negative number to positive in a data frame?

This can be done by using abs function. For example, if we have a data frame df with many columns and each of them having some negative values then those values can be converted to positive values by just using abs(df).

How do I write a query in pandas?

Pandas query syntax Assuming you have a DataFrame, you need to call . query() using “dot syntax”. Basically, type the name of the DataFrame you want to subset, then type a “dot”, and then type the name of the method …. query() .


1 Answers

I can reproduce this error on pandas v0.20.3 with specific data types; for example, np.float32. The workaround is to cast explicitly as float.

This is a known bug: DataFrame.eval errors with 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)
like image 150
jpp Avatar answered Oct 18 '22 10:10

jpp